DataStore not saving leaderstats

Hi fellow developers!

So I created a currency for my game called “DevPoints” and I saved it to the player’s leaderstats. In another script, I also created some variables and a loop to make the amount of DevPoints a player has rise by 3 every 5 seconds. Moreover, I attempted to save the entirety of this to the DataStoreService. I used print and pcall to help me, but for some reason, the DataStore didn’t work. I tried disabling the script that made the DevPoints increase, but that didn’t seem to work either. Plus, nothing printed in the output, so that made it even more confusing.

Both of these server scripts are located in the ServerScriptService. Here’s the DataStore script:

-- Create currency called "DevPoints" and put in player's leaderstats, then save to DataStore

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local devpoints = Instance.new("IntValue")
	devpoints.Name = "DevPoints"
	devpoints.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-devpoints")
	end)
	
	if success then
		devpoints.Value = data
	else
		print("An error occurred while retreiving your data.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-devpoints",player.leaderstats.DevPoints.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("An error occurred while saving your data.")
		warn(errormessage)
	end
	
end)

Not sure what’s wrong with it; everything seems correct. If you’d like to see the DevPoints increase script, just let me know, although I’m pretty sure it’s not needed to solve the DataStore issue.

Thanks for reading! :grinning:

1 Like

Your script looks good. If you’re doing Studio Testing, then it probably isn’t saving because that’s simply how studio testing works, it never ends up firing the “PlayerRemoving.” My guess is that if you publish and then check it out in the actual game, this will work fine.

You can do a studio test it by doing this:

Go to the command bar and paste in:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
myDataStore:SetAsync("YOUR USER ID HERE-devpoints",500)

(make sure to insert your user ID into “YOUR USER ID HERE”. Then, join the game in studio testing and see if your have 500 points. If so, then your script works.

2 Likes

oh I’ve been using it that way for about half a year now. I never came across any problems with losing data or something like that. Now you’re tell me i don’t know why i did that with the wait function. It doesnt even make sense LOL. Imma change it right now! Thanks for telling that flaw in my code

1 Like

Did you maybe mean to check for whether the stored value was nil, so you could avoid setting the Value to nil?

 local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-devpoints")
	end)

 data = data or 0
 something.Value = data

Remember to scatter around a few more print statements after and before making requests to see what values exactly are being saved and retrieved.

1 Like

Thanks for the advice! Next time, I’ll be sure to use more print statements so I can see exactly what went wrong.

As a side note if the data doesnt load you should tag them some sort of way for example a string value inside them called DATADIDNOTLOAD, and optionally kick them. And when the player is leaving check if they have that tag if they do then dont save. You should do this because right now if the data doesnt load all your script will do is warn and print something, so the leaderstats would be 0. So if the players data failed to load next time he joined he would have 0 points.