Why isn't my datastore saving?

local ds = game:GetService("DataStoreService"):GetDataStore("GoldStorage")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",stats)
	coins.Name = "Gold"
	coins.Value = ds:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
	wait(1)
	local stats = plr.leaderstats
	local gold = plr.Gold
	
	ds:SetAsync(plr.UserId,gold.Value)
end)

This my my script to save gold into the place I am using, I did a lot of research in the devforum for answers but it either was a different way that was too confusing, or was just generally confusing.

Can someone help me understand what I did wrong? I’m testing it IN-GAME and it still doesn’t work from when I join to leave.

1 Like

PlayerRemoving fires just before the player leaves, which is leaving you with very little time to save your data, on top of that you are waiting one additional second, please read this article about game:BindToClose(). I also recommend instead of using SetAsync() that you use UpdateAsync(), as it just updates the player data instead of setting the player’s data each time.

Also please make sure to use pcall, just so the script continues despite if an error gets thrown or not when loading or saving player’s data.

1 Like

IS API SERVICES ON? it has to be on or else it will not work

yes, use pcall that may be the reason its not working

another reason it may not work if your testing in studio

There is no reason to yield the thread, and Gold should be a member of leaderstats, not plr.

game.Players.PlayerRemoving:Connect(function(plr)
	wait(1)
	local stats = plr.leaderstats
	local gold = plr.Gold
	
	ds:SetAsync(plr.UserId,gold.Value)
end)

Another main reason is that if you’re testing this on Studio, the server shuts down when you leave as you’re the only player, therefore you also need to save data on server shutdown via BindToClose.

	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",stats)
	coins.Name = "Gold"
	coins.Value = ds:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local stats = plr.leaderstats
	local gold = stats.Gold
	
	ds:SetAsync(plr.UserId, gold.Value)
end)

game:BindToClose(function()
     for _, player in ipairs(game.Players:GetPlayers()) do
         coroutine.wrap(ds.SetAsync)(ds, player.UserId, player.leaderstats.Gold.Value)
     end
end)

@MarkiPr0

I suggest you to provide some context behind on why to use UpdateAsync over SetAsync. Your point lacks sufficient points.

i think you got your answers!!

Im using these answers, and none of them seem to be working…

look above the link, that will help

Can you use pcall in an example? The lua documentation wasn’t very clear.

This is the datastore that I use too. It always works

That tutorial is very confusing, I’ve tried using it but I just didn’t understand it. Like it works, but I have no idea how it works except for some portions. I like the way I am doing it, and it’s worked before this way like a week or two ago, but then it just stopped working.

In fact, I never understood it either. roblox should explain itself better with its tutorials. Try searching for datastores in this forum, maybe you can find some.

Yeah, but their new tutorials are pretty good, but this specific one is just extremely confusing. I searched the entire devforum and they either didn’t make sense or they weren’t my method.

Try changing your code to this:

local ds = game:GetService("DataStoreService"):GetDataStore("GoldStorage")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",stats)
	coins.Name = "Gold"
	coins.Value = ds:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
	wait(1)
	local stats = plr.leaderstats
	local gold = stats.Gold -- You put plr.Gold before
	
	ds:SetAsync(plr.UserId,gold.Value)
end)

i was having a LOT problems when i was learning datastores this `tutorial realy helped

`

My script already looks like that now, I changed it to stats.Gold.

i was having a LOT problems when i was learning datastores this `tutorial really helped

do you have API services enabled