Not able to save the game

So first im not a scripter but im trying to work for it! I am making my first game and I have watched AlvinBlox tutorial on how to make simulator games but I’m now stuck on the saving the game part where it save all the paper (wich is my kind of money in the game)and the other stuff in the leaderstats.
I have tried for many hour to understand what was wrong in my codes but since I have 0 experience in coding lua I found nothing and I need help. Here is my leaderstats codes. thanks :smiley:

local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local paper = Instance.new("NumberValue")
	paper.Name = "Paper"
	paper.Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = player.Name
	dataFolder.Parent = serverStorage.RemoteData
	
	local debounce = Instance.new("BoolValue")
	debounce.Name = "Debounce"
	debounce.Parent = dataFolder
	
	local paperData, rebirthsData
	
	local success,errormessage = pcall(function()
		paperData = DataStore:GetAsync("paper-"..player.UserId)
		rebirthsData = DataStore:GetAsync("rebirths-"..player.UserId)
	end)
	
	if success then 
		if paperData then
			paper.Value = paperData
			rebirths.Value = rebirthsData
		end
	end
	
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)

sorry for the english error I make I am french and make mistake a lot of the time but im working for it!

Are you testing this in a real game or in the studio? Also, you haven’t add an if success in the player removing.

1 Like

yes I have tested it in the real game and I should write it like that?

game.Players.PlayerRemoving:Connect(function(player)
        if success then
	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)

sorry im really new at coding and for the moment I just copied and paste what I saw in the tutorial I was watching but in my game it doesnt work even if im almost sure I wrote the same thing

Try adding

if success then
   print("Success")
else
   print("Failed")
end

after the

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

and tell me what does it print.
Also, remove the if paperData then in

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

because if it is success then just make paper value = paperData

The if paperdata is there most likely there to check if the player actually has data, so it prevents it from throwing an error about trying to set a value to nil

Hmm you’re right. (30 charsss)

the code is now

	if success then 
		
			paper.Value = paperData
			rebirths.Value = rebirthsData
		end
	end
	
end) ---this end) is underlined by red

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)

and printed nothing

I think you need to make a table or another DataStore to save more than one item. After saving the paper, the rebirth part overwrote it, so the game does not know the value of paper.

1 Like

I don’t think that is true cause he sets those 2 values into 2 different keys.

It’s all in the same one, he didn’t make 2 keys I think.

I know that if you are testing this in studio, the game could be closing faster than the data can save. I’m not too sure about the speed at which a non-studio server shuts down though. In that case, you would need a bind to close function

1 Like

I’ve encountered a similar issue in the past. When a player leaves the game, their Player object is quickly destroyed after the event has fired. As your leaderstats are linked to the player object, they are deleted.
SOLUTION: Do not store your leaderstats on the Player objects.

1 Like

You sure? Look at the string after SetAsync

Where else would you store the leaderstats?

Also, you should never try to test data saving in studio, because of what @DarkDanny04 said. ALWAYS test in-game.

You have to delete the end above.

You can store leaderstats wherever you like. I’d personally recommend putting them in ServerScriptService. Make a folder for each player with the folder’s name being the player’s name.

Hmm, that’s not the place you should put it. I guess the best place would be ServerStorage since the service name has Storage in it.

Just about any location will work fine.
However, ServerStorage specifically is better suited for items that will not be edited, such as models waiting to be spawned. Leaderstats are regularly changed and therefore should not be placed in ServerStorage.
ServerScriptService contains many active objects (such as scripts) and therefore makes a great place for leaderstats.

my leaderstats script are all printed in ServerScriptService

But I thought the player can’t see items in ServerStorage and ServerScriptService.
I usually put stuff in ReplicatedStorage so both the player and the server can see it.