Data not saving with datastores

Hello, I am making a test place for developing and I am currently testing out datastores.
However, the data cannot save.

The code:

local dss = game:GetService("DataStoreService")

local data = dss:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	
	
	local playerId = "Player_" .. plr.UserId
	
	local plrdata
	
	local success, error = pcall(function()
		plrdata = data:GetAsync("playerId")
	end)
	
	if success then
		cash.Value = plrdata
		
		print("Data loaded for player!")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local playerId = "Player_" .. plr.UserId
	
	local attempt = 0
	
	repeat
		local data = plr.leaderstats.Cash.Value
		
		local success, error = pcall(function()
			data:SetAsync(playerId, data)
		end)
		
		if success then
			print("Data saved for player!")
		else
			warn("Data could not be saved for player!")
		end
		
		attempt += 1
	until success or attempt == 5
end)

The console always gives the warning in the saving code. Any advice? I’m testing in Roblox Studio, if that matters at all.

What is the specific warning that you are getting?

It;s the one I put for the PlayerRemoving, it gives me that warning 5 times.
image

May sound silly, but you have indeed enabled API services in the experience, before troubleshooting further?

Yes I have.
image

Do this for us please:

if success then
	print("Data saved for player!")
else
	warn("Data could not be saved for player!")
	print(error)
end

I’m hedging my bets however that you probably need to enable access to the datastore in studio through settings. If not, the error will tell us the issue!

You really shouldn’t use “error” as the keyword is already occupied, but this will not be an issue in this context as you’ll overwrite it.

I think I declared 2 different variables under the same name; I will see how it works now that I have fixed that.

The data has saved, and it says it loads, but it isn’t loading any data from the previous session.

Found your problem, you need to use another variable then data as shown here:

game.Players.PlayerRemoving:Connect(function(plr)
	local playerId = "Player_" .. plr.UserId

	local attempt = 0

	repeat
		local mydata = plr.leaderstats.Cash.Value

		local success, errortxt = pcall(function()
			data:SetAsync(playerId, mydata)
		end)

		if success then
			print("Data saved for player!")
		else
			warn("Data could not be saved for player!")
			print(errortxt)
		end

		attempt += 1
	until success or attempt == 5
end)

Loading data used a string that is playerId, when it should have used the variable playerId.

I fixed that simple error and everything seems to be working now. Thanks for the help though.

1 Like

Just a bit of a advice from some of my time saving data, I would add this to your code:

game.Close:Connect(function()
	wait(3)
end)

Gives the game time to process data on the player without issues.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.