View Single Post
11-25-23, 08:31 PM   #1
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Why doesnt the font string update?

This is my .toc file
## Title: LastLoginTracker
## Version: 1.0
## Interface: 90005
## Author:
## SavedVariablesPerCharacter: LastLoginTrackerDB

LastLoginTracker.lua


and here is the .lua file
-- LastLoginTracker.lua

local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")

-- Function to initialize or load saved last login time
local function InitializeLastLogin()
if not LastLoginTrackerDB then
LastLoginTrackerDB = {}
end

if not LastLoginTrackerDB.lastLoginTime then
LastLoginTrackerDB.lastLoginTime = time()
end
end

-- Function to update and display last login time
local function UpdateLastLogin()
LastLoginTrackerDB.lastLoginTime = time()
local formattedTime = date("%H:%M:%S", LastLoginTrackerDB.lastLoginTime)
print("Last Login: " .. formattedTime)
frame.LastLoginTrackerTextLabel:SetText("Last Login: " .. formattedTime)
end

-- Create a simple form with a button
local frame = CreateFrame("Frame", "LastLoginTrackerFrame", UIParent, "BasicFrameTemplate")
frame:SetSize(250, 100)
frame:SetPoint("CENTER")
frame:Hide()

frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
frame.title:SetPoint("CENTER", frame.TitleBg, "CENTER", 0, 0)
frame.title:SetText("Last Login Tracker")

-- Create a button to update last login time
local LastLoginTrackerButton = CreateFrame("Button", nil, frame, "GameMenuButtonTemplate")
LastLoginTrackerButton:SetSize(200, 25)
LastLoginTrackerButton:SetPoint("CENTER", frame, "CENTER", 0, 0)
LastLoginTrackerButton:SetText("Update Last Login")
LastLoginTrackerButton:SetNormalFontObject("GameFontNormal")

-- Create a text label to display last login time
LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
LastLoginTrackerTextLabel:SetPoint("CENTER", frame, "CENTER", 0, -20)
LastLoginTrackerTextLabel:SetText("Last Login: Not tracked")

-- Set up event handling for the button
LastLoginTrackerButton:SetScript("OnClick", function(self, button, down)
UpdateLastLogin()
end)

-- Event handler for PLAYER_LOGIN
frame:SetScript("OnEvent", function(self, event, arg1)
if event == "PLAYER_LOGIN" then
InitializeLastLogin()
end
end)

-- Slash command to show/hide the form
SLASH_LASTLOGINTRACKER1 = "/lastlogin"
SlashCmdList["LASTLOGINTRACKER"] = function()
if frame:IsShown() then
frame:Hide()
else
frame:Show()
end
end

When I click on the button the font string LastLoginTrackerTextLabel isn't updated.
I'm doing something wrong but can't figure out what it is.
I thought it might be related to local vs global variable scoping but I'm new enough to lua that I can't figure it out.

Any help would be appreciated.
  Reply With Quote