DataStore isn't saving Players points

  1. Want the game to save player data after leaving and retrieve it back.

  2. The issue is the player data isn’t saving. Join this game, click the cube then rejoin and you will see the error. Datastore test - Roblox

  3. The script is fine, I see no errors in output when I Test it. I’m confused why its not working. Please help.

I have re-read the script, and I found my own error and fixed the datastore saving. It works successfully now. Thanks for your help guys.

1 Like

Send the script, i will check for any issues.

1 Like

Okay I will send a message to you with it…

Hmm, I don’t see any issues but try this shorter script.

local ds = game:GetService("DataStoreService")
local ds1Key = ds:GetDataStore("DS")
game.Players.PlayerAdded:Connect(function(plr)
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"	

	local Platinum = Instance.new("IntValue", folder)
	Platinum.Name = "Platinum"
	
	local Rubies = Instance.new("IntValue",folder)
	Rubies.Name = "Rubies"
	
	Platinum.Value = ds1Key:GetAsync(plr.UserId) or 0
	Rubies.Value = ds1Key:GetAsync(plr.UserId) or 0
	
	ds1Key:SetAsync(plr.UserId, Platinum.Value)
	ds1Key:SetAsync(plr.UserId, Rubies.Value)
	
	Platinum.Changed:Connect(function()
		ds1Key:SetAsync(plr.UserId, Platinum.Value)	
	end)	
	
	Rubies.Changed:Connect(function()
		ds1Key:SetAsync(plr.UserId, Rubies.Value)	
 end)
end)

Alright I will try it, thanks for helping.

1 Like

Shouldn’t you use SetAsync In player removing event?

1 Like

I’m not sure if this is a copy pasta typo but you don’t have a folder to parent the values

Whoops, i forgot to put that in, thanks!

btw I recommend switching to DataStore2 or ProfileService

Much more efficient, easy to use and less chance of data loss.

3 Likes

Something i just noticed is your using same keys for both DataStores so it’s overwriting the data everytime with the other value

Yeah nice catch Use plr.UserId…“Platinum” and plr.UserId…“Rubies”.

1 Like

i make a lot of issues :(. its kinda late where i am

Don’t give up at first my datastores never worked but they do now (sometimes lol)

1 Like

Man I’m super new to datastore as well Lol…

Could you send the full script without modifications in this thread?

Regarding your script, it’s wrong.

  1. Use pcall functions while GetAsync data’s.
  2. Only use SetAsync when a player leaves.

Then you should watch tutorials about DataStoreService.

1 Like

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Money.Value; -- First value from the table
		player.leaderstats.Coins.Value -- Second value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Platinum = Instance.new("IntValue")
	Platinum.Name = "Platinum"
	Platinum.Parent = leaderstats

	local Rubies = Instance.new("IntValue")
	Rubies.Name = "Rubies"
	Rubies.Parent = leaderstats

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success then -- If there were no errors and player loaded the data

		Platinum.Value = data[1] -- Set the money to the first value of the table (data)
		Rubies.Value = data[2] -- Set the coins to the second value of the table (data)

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player)-- When a player leaves
	local data
	local success, err  = pcall(function()
		saveData(player) -- Save the data
		
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

For starters, since you already call a pcall function in the saveData function, there’s no need to wrap that function in a pcall.

Also, when you actually GET the player’s data, it’s much better to use a table as the initial value.

This is your script:

It should look like:

data = dataStore:GetAsync(player.UserId) or {0, 0}

for more flexibility for changing starting values for the player. This datastore in general is just really messy and you should start looking for more effective and safe ways to save a player’s data.


If you haven’t done this, you should turn on studio access to API services in game setting.