View Single Post
11-26-23, 09:39 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,892
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:
  1. -- LastLoginTracker.lua
  2.  
  3. -- Function to initialize or load saved last login time
  4. local function InitializeLastLogin()
  5.     if not LastLoginTrackerDB then
  6.         LastLoginTrackerDB = {}
  7.     end
  8.  
  9.     if not LastLoginTrackerDB.lastLoginTime then
  10.         LastLoginTrackerDB.lastLoginTime = time()
  11.     end
  12. end
  13.  
  14. -- Function to update and display last login time
  15. local function UpdateLastLogin(self)
  16.     LastLoginTrackerDB.lastLoginTime = time()
  17.     local formattedTime = date("%H:%M:%S", LastLoginTrackerDB.lastLoginTime)
  18.     print("Last Login: " .. formattedTime)
  19.     self.LastLoginTrackerTextLabel:SetText("Last Login: " .. formattedTime)
  20. end
  21.  
  22. -- Create a simple form with a button
  23. local frame = CreateFrame("Frame", "LastLoginTrackerFrame", UIParent, "BasicFrameTemplate")
  24. frame:SetSize(250, 100)
  25. frame:SetPoint("CENTER")
  26. frame:Hide()
  27. frame:RegisterEvent("PLAYER_LOGIN")
  28.  
  29. frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  30. frame.title:SetPoint("CENTER", frame.TitleBg, "CENTER", 0, 0)
  31. frame.title:SetText("Last Login Tracker")
  32.  
  33. -- Create a button to update last login time
  34. frame.LastLoginTrackerButton = CreateFrame("Button", nil, frame, "GameMenuButtonTemplate")
  35. frame.LastLoginTrackerButton:SetSize(200, 25)
  36. frame.LastLoginTrackerButton:SetPoint("CENTER", frame, "CENTER", 0, 0)
  37. frame.LastLoginTrackerButton:SetText("Update Last Login")
  38. frame.LastLoginTrackerButton:SetNormalFontObject("GameFontNormal")
  39.  
  40. -- Create a text label to display last login time
  41. frame.LastLoginTrackerTextLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  42. frame.LastLoginTrackerTextLabel:SetPoint("CENTER", frame, "CENTER", 0, -20)
  43. frame.LastLoginTrackerTextLabel:SetText("Last Login: Not tracked")
  44.  
  45. -- Set up event handling for the button
  46. frame.LastLoginTrackerButton:SetScript("OnClick", function(self, button, down)
  47.     UpdateLastLogin(self:GetParent())
  48. end)
  49.  
  50. -- Event handler for PLAYER_LOGIN
  51. frame:SetScript("OnEvent", function(self, event, arg1)
  52.     if event == "PLAYER_LOGIN" then
  53.         InitializeLastLogin()
  54.     end
  55. end)
  56.  
  57. -- Slash command to show/hide the form
  58. SLASH_LASTLOGINTRACKER1 = "/lastlogin"
  59. SlashCmdList["LASTLOGINTRACKER"] = function()
  60.     frame:SetShown(not frame:IsShown())
  61. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-26-23 at 09:43 AM.
  Reply With Quote