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

DataStore not saving money on leave and join?

I Have a money system on my game that gives each player a certain amount of money every 10 seconds. The code for that localscript is below. BRB is the Instance with the money value.

while true do
	wait(10)
	game.Players.LocalPlayer.BRB.Value = game.Players.LocalPlayer.BRB.Value + 10
end

I recently made a Datastore script and placed it in serverscriptservice. This script is supposed to save every player's money whenever they leave and join the game so no money is lost. For some reason the script is not saving anything at all. I cannot seem to figure out what I did wrong. Code for that script below:

local DataStore = game:GetService("DataStoreService")
local Points = DataStore:GetDataStore("PointSave")

game.Players.PlayerAdded:connect(function(p)
	local BankRollBux = Instance.new("IntValue")
	BankRollBux.Parent = p
	BankRollBux.Name = "BRB"
	BankRollBux.Value = Points:GetAsync(p.UserId) or 0
	Points:SetAsync(p.UserId, BankRollBux.Value)
	BankRollBux.Changed:connect(function()
		print("Saving")
		Points:SetAsync(p.UserId, BankRollBux.Value)

		print("Saved")
	end)
end)
game.Players.PlayerRemoving:connect(function(p)
	Points:SetAsync(p.UserId, p.BRB.Value)

end)

1 answer

✓ Accepted answer

Your DataStore is most likely saving data properly, however the issue is that you're giving the player points from the client. When you do something important like that from a LocalScript, the server can't see it. So according to the server, players still have 0 points. Try awarding the player points from a server script instead.

Also keep in note sometimes your data won't save properly when testing in studio as when you click 'Stop', the session closes immediately, not giving any time to save the data, whereas in a game there will be a bit more time before the session closes. So before thinking it isn't working, test it in-game.

1 comment

1

Thank you, just did that and now it works perfectly. Appreciated.

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