Need help with a Data Store script

Hi, so this is my first time working with Data stores. I have read documentation but here I have a problem that I can’t solve alone.
I made a script which should get 2 values from Data Stores (or initialize the variables if there is no value for the player) and save those 2 values when the player leaves.

Here is the script
local DataStoreService = game:GetService("DataStoreService")
local checkpointStore = DataStoreService:GetDataStore("PlayerCheckpoint")
local moneyStore = DataStoreService:GetDataStore("MoneyStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	-- get checkpoint Data
	local success, currentCheckpoint = pcall(function()
		return checkpointStore:GetAsync(player)
	end)
	if success then
		print("Current Checkpoint:", currentCheckpoint)
		local checkPoint = Instance.new("IntValue", player)
		checkPoint.Name = "CheckPoint"
		checkPoint.Value = currentCheckpoint
	else
		print ("Checkpoint initialized")
		local checkPoint = Instance.new("IntValue", player)
		checkPoint.Name = "CheckPoint"
		checkPoint.Value = 0
	end
	
	-- get money Data
	local success, currentMoney = pcall(function()
		return moneyStore:GetAsync(player)
	end)
	if success then
		print("Current Money:", currentMoney)
		local money = Instance.new("IntValue", player)
		money.Name = "Money"
		money.Value = currentMoney
	else
		print("Money initialized")
		local money = Instance.new("IntValue", player)
		money.Name = "Money"
		money.Value = 0
	end
end)

Players.PlayerRemoving:Connect(function(player)
	-- set checkpoint Data
	local playerCheckpoint = player.CheckPoint.Value
	local success, err = pcall(function()
		checkpointStore:SetAsync(player, playerCheckpoint)
	end)
	if success then
		print("Checkpoint save Success")
	else
		print("Checkpoint save Error")
	end
	
	-- set money Data
	local playerMoney = player.Money.Value
	local success, err = pcall(function()
		moneyStore:SetAsync(player, playerMoney)
	end)
	if success then
		print("Money save Success")
	else
		print("Money save Error")
	end
end)

The first part is working fine so far (it’s initializing my 2 variables).
But when I leave the game, nothing happens. It doesn’t print anything although I asked to print something whether it works or not.

I don’t know what’s wrong, I repeat it’s my first time working with Data Stores. If anyone can help it would be awesome.

Sorry for the long script, I thought it would be easier to understand with the entire code.
Sorry of I made English mistakes, it’s not my first language. Don’t hesitate to tell me.

Sometimes testing in Studio doesn’t work. Test this in Roblox Player and it should work fine.

Thank’s for the reply, just one question then. Can I see the output somehow while testing in Roblox Player?

If you push F9 it should open the Developer Console.

It may not work in Roblox Studio. This is because servers shut down when in Studio. Try this:

game:BindToClose(function()
for _, Plr in pairs(game.Players:GetPlayers()) do
DataStore:SetAsync(PlayerKey, Data)
end
end)

Are you sure that you opened access to API Services(or whatever it’s called)?

You only need this for testing in studio—and it’s never advised because studio uses the same servers as the in-game datastore meaning data can get overwritten easily.

Ways to prevent this

  • Use UpdateAsync with a DataVersion integer instead (which you should already be doing)
  • Make a testing place.

For the BindToClose function, you should be wrapping each individual player-specific save method in coroutine. Since you only have 30 seconds, you don’t want to have to wait for one player’s data to finish saving before you start the next one. SetAsync is a yielding function.

Also, use UpdateAsync.

Thank you all for your replies, as @ Ty_Scripts told me, sometimes it doesn’t work in studio.
I tested it with Roblox Player and it’s working totally fine!

Have a nice day / night everyone :grin:

Press F9 or say “/console” in the chat window. :grin:

1 Like