Not able to save the game

@jenesuispasunevache That’s great. Next, move the actual leaderstats object to ServerScriptService as well. Right now, they’re on the player, meaning they are deleted when the player leaves the game, which can cause issues.

@RealJefff2000 The player doesn’t need to have direct access to their leaderstats objects, as the server handles all calculations regarding them. In fact, placing the leaderstats in ReplicatedStorage could open the door for hacking stats.
If the local player needs to have access to the values, a RemoteEvent will suffice – the actual leaderstat objects should strictly not be placed in ReplicatedStorage.

I did it and still printed nothing and now its the game that is underline in red in

	if success then 
	
			paper.Value = paperData
			rebirths.Value = rebirthsData
		end
	end
	

--- this one (under the this)
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
		DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
	end)
	if success then
   print("Success")
else
   print("Failed")
	end
end)

Nothing appears to be underlined in red. What is supposed to be underlined?

I meant the end not the end)
So the script should be:

	if success then 
	
			paper.Value = paperData
			rebirths.Value = rebirthsData
		end
	--Delete this one instead
	

end)
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
		DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
	end)
	if success then
   print("Success")
else
   print("Failed")
	end
end)

well at least there were no error message :smiley: but there is nothing printed

I believe this is an issue with your testing in Studio. Use the ‘start local server’ option and see what happens.

You might not be able to see the print because you left before it was printed.

1 Like

The game is probably shutting down before the PlayerRemoving function gets a chance to finish. Try hooking a function that yields a while to BindToClose.

3 Likes

I just tested it in the real roblox out of curiosity and it saved!!! :tada: :smiley:

Do this:
Join a server, open Dev Console [F9]
Then click server is logs, type in this:
game.Players.[Your name].leaderstats.Paper.Value = 1000
Then leave and rejoin. If it works, it works, if it doesn’t then you have to fix that line.

so in summary what I had to do was

if success then 

		if paperData then  ---delete that

			paper.Value = paperData
			rebirths.Value = rebirthsData
		end
	end --- and that
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		DataStore:SetAsync("paper-"..player.UserId,player.leaderstats.Paper.Value)
		DataStore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
	end)
end)

A big nice thank you to all the people who helped me out!! :smiley: