Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Not all data is saving?

I have a script that saves player's data but, it only saves Kills not money or units. When I tested this in game with someone else this bug didn't happen only when I was alone (No, it wasn't in studio test mode, it was in game), so I guess what causes this is the server ends before the script can save the player's data. How do I fix this??

game.Players.PlayerRemoving:connect(function(plrleaving)
	local success, message = pcall(function()
    	local PlayerKey = "user_"..plrleaving.userId
		local data = game.ServerStorage.SAVEDDATA[plrleaving.Name]
	    KillsData:SetAsync(PlayerKey, data.Kills.Value)
		MoneyData:SetAsync(PlayerKey, data.Money.Value)
		local units = {}
		for number, unit in pairs (data.Units:GetChildren()) do
			units[number] = unit.Value
		end
		UnitData:SetAsync(PlayerKey, units)
		data:Destroy()
		end)
	if not success then
	    print("An error occurred with saving "..plrleaving.Name.."'s data!")
	end
end)

1 answer

✓ Accepted answer

Make the server wait

Set the OnClose callback to make the server wait for a few seconds.

game.OnClose = function() wait(5) end;

This discussion is preserved as it was. New answers, comments, and votes are closed.