Datastore not working

Hello, I am trying to make a money datastore, but for some reason it doesnt work.

Script:

local DSS = game:GetService("DataStoreService")
local coinSave = DSS:GetDataStore("CoinSave")

game.Players.PlayerAdded:Connect(function(plr)
	local cash = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
	
	local data
	local success, err = pcall(function()
		data = coinSave:GetAsync(plr.UserId)
	end)
	
	if success then
		cash.Value = data
		print("Coin data loaded")
	else
		print("Data not loaded")
	end
end)

local function saveData(plr)
	local cash = plr:WaitForChild("leaderstats"):WaitForChild("Credits")
	
	local data
	local success, err = pcall(function()
		data = coinSave:SetAsync(plr.UserId, cash.Value)
	end)
	
	if success then
		print("Coin Data saved")
	else
		print("Coin Data not saved")
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

game:BindToClose(function()
	for i, v in pairs(game.Players:GetPlayers()) do
		saveData(v)
	end
end)

According to the prints everything goes correctly

you reference

local cash = plr:WaitForChild("leaderstats"):WaitForChild("Credits")

Where is this instance created?

In a different script.

30characters

You’re saving their data, but not getting it when they join.

image

Oh, I didn’t see that. You’re getting their sync, but you didn’t specify what - only their UserId. I’d recommend changing it to:

		data = coinSave:GetAsync(plr.UserId.."-coins")
		data = coinSave:SetAsync(plr.UserId.."-coins", cash.Value)
1 Like

Have you tried changing the Coins Value on the Server-Side?

There’s an unecessary line on the local function

local data
	local success, err = pcall(function()
		data = coinSave:SetAsync(plr.UserId, cash.Value)
    end)

-- change it to

	local success, err = pcall(function()
		coinSave:SetAsync(plr.UserId, cash.Value)
    end)

Make sure API services are on in your game settings

1 Like