The DataStore API on open cloud doesn't work

Hi, I was writing a script to save user’s money, but it doesnt work. I tried everything I can to fix it, but I can’t, can anyone help? Here are more details:

  1. Goal: I wanted to make a script that will save user’s cash
  2. Issue: it can’t get the cash value:

  1. What I tried to do to fix it: I tried to change the cash value from IntValue to NumberValue, I tried to manually change cash value, And I gave the API key all privileges on the creator dashboard.

I dont know whats the problem, i hope you can help me! Here is the code:

local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore('SaveMoney')
local DataStoreApiKey = 'MyApiKey' -- here is the api key that i generated at create.roblox.com, i just replaced it with this text so i dont leak my API Key.

game.Players.PlayerAdded:Connect(function(player)
	local cash = player:WaitForChild("cash")
	local data = nil
	local sucess, errormsg = pcall(function()
		data = DataStore:GetAsync(DataStoreApiKey)
	end)
	if sucess then
		cash.Value = data
		print(data) -- output is 0
		print('data is saved')
	end
	if errormsg then
		warn(errormsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local cash = player:WaitForChild("cash")
	print(cash.Value) -- output is 0
	local data = cash.Value
	print(data) -- output is 0
	
	local success, errormsg = pcall(function()
		DataStore:SetAsync(DataStoreApiKey, data)
	end)
	
	if success then
		print("saved")
	end
	if errormsg then
		warn(errormsg)
	end
end)

Thanks for your help!
If you need the script where i created the NumberValue for cash tell me in comments.

You can try this version of your code

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("SaveMoney")
--Add what you need

game.Players.PlayerAdded:Connect(function(player)
	-- local leaderstats = Instance.new("Folder")
	--leaderstats.Parent = player
	--leaderstats.Name = "leaderstats"
	
   local Cash - Instance.new("NumberValue")
--	local Cash= Instance.new("IntValue")
--	Cash.Parent = leaderstats
    Cash.Parent = player
	Cash.Name = "Cash"

	local tableOfData
	local success, errormessage = pcall(function()
		tableOfData = datastore:GetAsync(player.UserId)
	end)
	if success and tableOfData then -- we know that it is a returning player whom already played this game before
		for key, value in pairs(tableOfData) do
			print(key)
			print(value)
			Cash.Value = tableOfData["Cash"]
		end
	else -- we know that this player is new or their data got throttled (cached) or whatever you call it and have lost their data
		--sets the default value for new players

		Cash.Value = 0


	end

end)
game.Players.PlayerRemoving:Connect(function(player)

	local tableOfData = {
		-- ["Cash"] = player.leaderstats.Cash.Value,
         ["Cash"] = player.Cash.Value,
	}

	local success, errormessage = pcall(function()
		datastore:SetAsync(player.UserId, tableOfData)
	end)
end)

1 Like

Thanks, I will try it now. Hope it wokrs!

Maybe it’s too late, but I think it’s worth asking, when you receive money, does this function go through FireServer?Maybe this is your problem, you can try to add money to yourself through the developer console and exit the game, thereby checking whether data saving works

This command is in the console:

game.Players.[PlayerName].leaderstats.Cash.Value = 9999

(I just see that this topic is still not closed, so I think that maybe I can help you somehow)

1 Like