I put strength, intelligence, defense, and health stat values in a folder in the player call playerstats and in another local script when a player levels up and adds points to that stat its suppose to go up which it does when I check the value, but when I try to save it using a script in serverscriptservice the values show up as 0.
local function savePointAllocation(player) local playerstats = player.PlayerStats local str = playerstats:FindFirstChild("Strength") local def = playerstats:FindFirstChild("Defense") local int = playerstats:FindFirstChild("Intelligence") local hp = playerstats:FindFirstChild("Health") local success, err = pcall (function() local newStatValues = { Strength = str.Value, Defense = def.Value, Intelligence = int.Value, Health = hp.Value } plrStats:SetAsync(player.UserId.."Stats",newStatValues) end) if success then print("Point allocation successfully saved!") elseif not success then repeat print(err) local newStatValues = { Strength = str.Value, Defense = def.Value, Intelligence = int.Value, Health = hp.Value } plrStats:SetAsync(player.UserId.."Stats",newStatValues) until success end end
game.Players.PlayerRemoving:Connect(function(player) savePointAllocation(player) end)
The classic you forgot game:BindToClose()
.
Basically before your datastore can successfully store all data the game server has already shutdown and terminates the datastore process.
However if you use game:BindToClose()
, loop through all remaining players in the game and save the data for each of the players at that point, then the game will wait until the saving has been handled and then exit the game.
The wiki explains this exact issue aswell:
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose