I made a datastore script that saves the IntValues in the leaderstats when the player leaves. This does not work because the contents in the player are removed when the event is fired, it says "Level is not a valid member of Player." How would I make it save the stats of the player without getting an error? Table for each plr?
Hi Noobe,
:BindToClose()
method.function save_data(plr) local stats = plr:WaitForChild("Stats"); local tab_to_save, key = {}, "user-"..plr.UserId; for _, stat in next, stats:GetChildren() do tab_to_save[stat.Name] = stat.Value; end pcall(function() ds:SetAsync(key, tab_to_save); end) end
:BindToClose()
method to have a callback function save the data right away as the server is shutting down.function player_removing(plr) wait(0.001); if not is_closing_game then save_data(plr) end end game:BindToClose(function() is_closing_game = true; for _, plr in next, plrs:GetPlayers() do save_data(plr); end end)
player_removing
function is so that is_closing_game
can update if the game is closing. So, the player_removing
function doesn't try to save the data because the callback function in :BindToClose()
is already on it.Thanks,
Best regards,
~~ KingLoneCat
I typically keep an extra copy of everyone's data in ServerStorage for any data that needs to be stored. When a player joins the game just have it make them a Value in ServerStorage, and then when they leave you can save it and delete it.