Install
BugGrabber and
BugSack so you can see the errors you are getting.
You've created LastLoginTrackerTextLabel as a global variable
Code:
LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
But you're accessing it in UpdateLastLogin() as if it was created as a sub-key of frame
Three problems:
1. UpdateLastLogin can't access frame because it is a local variable created AFTER the function has been declared ie. it is out-of-scope
2. Once one is fixed you would need to change LastLoginTrackerTextLabel to be a sub-key of frame.
3. You're redeclaring the
frame variable to create a second unnecessary frame (well, the first is unnecessary as
LastLoginTrackerFrame can do event registration/handling).
The code adjusted to pass self:GetParent() (The parent of the button which is
frame) to the functions and use that. Also remove the first frame as the as you were registering the event on that one but setting the OnEvent script on the second,
LastLoginTrackerFrame (through redeclaring the
local frame variable) meaning PLAYER_LOGIN would never have fired.
Lua Code:
-- LastLoginTracker.lua
-- 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(self)
LastLoginTrackerDB.lastLoginTime = time()
local formattedTime = date("%H:%M:%S", LastLoginTrackerDB.lastLoginTime)
print("Last Login: " .. formattedTime)
self.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:RegisterEvent("PLAYER_LOGIN")
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
frame.LastLoginTrackerButton = CreateFrame("Button", nil, frame, "GameMenuButtonTemplate")
frame.LastLoginTrackerButton:SetSize(200, 25)
frame.LastLoginTrackerButton:SetPoint("CENTER", frame, "CENTER", 0, 0)
frame.LastLoginTrackerButton:SetText("Update Last Login")
frame.LastLoginTrackerButton:SetNormalFontObject("GameFontNormal")
-- Create a text label to display last login time
frame.LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
frame.LastLoginTrackerTextLabel:SetPoint("CENTER", frame, "CENTER", 0, -20)
frame.LastLoginTrackerTextLabel:SetText("Last Login: Not tracked")
-- Set up event handling for the button
frame.LastLoginTrackerButton:SetScript("OnClick", function(self, button, down)
UpdateLastLogin(self:GetParent())
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()
frame:SetShown(not frame:IsShown())
end