Thread Tools Display Modes
Prev Previous Post   Next Post Next
03-22-24, 09:25 AM   #1
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Saved variables not working

I've created a very simple addon to see how saved variables work but the saved variables are not working.
Here is what I have so far:

My .toc file:

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

and my lua code is:

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 = time
  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

When I login I see these messages:
My AddonDB was nil
Lastlogofftimeloaded: nil

and after logging out, the contents of the saved variable file 'MyAddon.lua' is:
MyAddonDb = nil

I've looked at so many posts but can't seem to figure out what I'm doing wrong.
I've logged out of my character and deleted the WTF saved variables file - didn't help.
I've restarted my computer in case the file was somehow preventing writing but no help.
I appreciate any help you can provide.
  Reply With Quote
 

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Saved variables not working


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off