Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I save player cash with DataStore?

What I'm trying to do is save the player's cash I have never used DataStore before so I have no idea what I'm doing.

Here is my attempt:

local DataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
	repeat
	local Cash = DataStoreService:GetDataStore(plr.Name.. "'s cash")

	local success, CashAmount = pcall(function()
		return Cash:GetAsync(plr.Name.. "'s cash")
	end)

	if success then
		print("Current Cash:", CashAmount)
		plr.PlayerScripts.Cash.Value = math.floor(CashAmount / 2)
	else
		repeat
		local success, err = pcall(function()
			Cash:SetAsync(plr.Name.. "'s cash", plr.PlayerScripts.Cash.Value)
		end)

		if success then
			print("Success!")
		else
			print("Failed!")
			end
			until success
		end
	until success
end)

game.Players.PlayerRemoving:Connect(function(plr)
	repeat
	local Cash = DataStoreService:GetDataStore(plr.Name.. "'s cash")

	local success, newName = pcall(function()
		return Cash:UpdateAsync(plr.Name.. "'s cash", plr.PlayerScripts.Cash.Value)
	end)

	if success then
		print("Updated ".. plr.Name .."'s Cash")
	else
		print("Failed!")
		end
	until success
end)

It repeatedly prints out "Failed!" In the output when I playtest it on Roblox Studio I have tried changing the script by adding the repeat loop to make sure it doesn't fail

It is a regular Script and is inside ServerScriptService

4 comments

0

Are you testing this in Studio? If so, do you have Studio access to API services enabled?

0

No I did not, Thanks ill try if it works now.

0

This fixed my problem, Thanks.

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.