View Single Post
03-22-24, 09:53 AM   #3
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Saved variables work now

Thank you Fizzlemizz!!
I changed the time to a date function and renamed the db file in the toc.

Here is the final code, toc file first:
Lua Code:
  1. ## Title: MyAddon
  2. ## Interface: 100206
  3. ## Version: 1.0
  4. ## Author:
  5. ## Notes: A simple WoW addon using a database
  6. ## SavedVariablesPerCharacter: MyAddonDB
  7. MyAddon.lua

Lua Code:
  1. -- Create a saved variable to store last time
  2. local lastLogoffTime = nil
  3.  
  4. -- Create a new frame
  5. local frame = CreateFrame("Frame")
  6.  
  7. -- Load last logoff time from saved variable
  8. local function LoadLastLogoffTime()
  9.     if MyAddonDB then
  10.         lastLogoffTime = MyAddonDB.lastLogoffTime
  11.         print("Last logoff time loaded:", lastLogoffTime)
  12.     else
  13.         print("Trying to load logoff time but MyAddonDB is nil.")
  14.     end
  15. end
  16.  
  17. -- Save last time to saved variable
  18. local function SaveLastLogoffTime(time)
  19.     if not MyAddonDB then
  20.         print("Trying to save logoff time but MyAddonDB is nil")
  21.     else
  22.         MyAddonDB.lastLogoffTime = date("%m/%d/%y %H:%M:%S")
  23.     end
  24. end
  25.  
  26. -- Hook the Logout event to update last logoff time
  27. frame:RegisterEvent("PLAYER_LOGIN")
  28. frame:RegisterEvent("PLAYER_LOGOUT")
  29. frame:SetScript("OnEvent", function(self, event)
  30.     if event == "PLAYER_LOGIN" then
  31.         if not MyAddonDB then
  32.             MyAddonDB = {}
  33.             print ("MyAddonDB was nil")
  34.         end
  35.         LoadLastLogoffTime()
  36.     elseif event == "PLAYER_LOGOUT" then
  37.         SaveLastLogoffTime()
  38.     end
  39. end)
  40.  
  41. -- Slash command handler function
  42. local function SlashCommandHandler(msg)
  43.     LoadLastLogoffTime()
  44. end
  45.  
  46. -- Register slash command
  47. SLASH_MYADDON1 = "/myaddon"
  48. SlashCmdList["MYADDON"] = SlashCommandHandler
  Reply With Quote