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:
## Title: MyAddon
## Interface: 100206
## Version: 1.0
## Author:
## Notes: A simple WoW addon using a database
## SavedVariablesPerCharacter: MyAddonDB
MyAddon.lua
Lua Code:
-- Create a saved variable to store last time
local lastLogoffTime = nil
-- Create a new frame
local frame = CreateFrame("Frame")
-- Load last logoff time from saved variable
local function LoadLastLogoffTime()
if MyAddonDB then
lastLogoffTime = MyAddonDB.lastLogoffTime
print("Last logoff time loaded:", lastLogoffTime)
else
print("Trying to load logoff time but MyAddonDB is nil.")
end
end
-- Save last time to saved variable
local function SaveLastLogoffTime(time)
if not MyAddonDB then
print("Trying to save logoff time but MyAddonDB is nil")
else
MyAddonDB.lastLogoffTime = date("%m/%d/%y %H:%M:%S")
end
end
-- Hook the Logout event to update last logoff time
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_LOGOUT")
frame:SetScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" then
if not MyAddonDB then
MyAddonDB = {}
print ("MyAddonDB was nil")
end
LoadLastLogoffTime()
elseif event == "PLAYER_LOGOUT" then
SaveLastLogoffTime()
end
end)
-- Slash command handler function
local function SlashCommandHandler(msg)
LoadLastLogoffTime()
end
-- Register slash command
SLASH_MYADDON1 = "/myaddon"
SlashCmdList["MYADDON"] = SlashCommandHandler