Currency saving script not working

I am trying to make a script that saves my currency “PawCoins”, but every time I leave and come back it resets the leaderstats back to zero. I have studio API access on.

Here is the script I am currently trying to use. It is a tutorial script because I am new to scripting. Ignore the comments I use those to keep track of what does what because I’m forgetful.

-- saves coin data

local DataStoreService = game:GetService("DataStoreService")

local BeachsideDataStore = DataStoreService:GetDataStore("BeachsideDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local PawCoins = Instance.new("IntValue")
	PawCoins.Name = "PawCoins"
	PawCoins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		BeachsideDataStore:GetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
	end)
	
	if success then
		PawCoins.Value = data
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	local success, errormessage = pcall(function()
		BeachsideDataStore:SetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
	end)
	
	if success then -- if data saved successfully
		print("Player Data Successfully Saved!")
	else -- if not
		print("There Was An Error When Saving Data")
		warn(errormessage)
	end
	
end)

I also tried adding this onto the end:

game:BindToClose(function()
	
	--when game is ready to shutdown
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("The game is shutting down, come back soon!")
		end
	end
	local success, errormessage = pcall(function()
		BeachsideDataStore:SetAsync(Player.UserId.."-PawCoins",Player.leaderstats.PawCoins.Value)
	end)
	
	if success then

		print("Closing data saved successfully!")
	end
	wait(5)
	
end)

I ended up removing this because it didn’t even work on this script.

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local PawCoins = Instance.new("IntValue")
	PawCoins.Name = "PawCoins"
	PawCoins.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = BeachsideDataStore:GetAsync(Player.UserId.."-PawCoins") -- You don't have to reference it its already set
	end)
	
	if success then
		PawCoins.Value = data
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
end)

It should work
The problem was that data is equale to nothin
so it was saving the money but nothing was set so there was nothin to set
also :GetAsync returns the value you set when you used the key

1 Like

Thank you! I’ll test it as soon as I get a chance.

It’s loading correctly now but it still won’t save. It prints my error message “There was an error when saving data” when I leave.