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

Money Datastore Not Saving Value Changes When A Player Leaves?

Hello! So I have this datastore script and it works as intended but when I make this other GUI that is supposed to deduct a specified value from the player's cash, it does as I want it to but the changes to the player's cash value do not save when the player leaves the game. What is wrong with the 2nd script? (This is FE)

Datastore Script

local DSService = game:GetService('DataStoreService'):GetDataStore('Money')
game.Players.PlayerAdded:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue')
	leaderstats.Name = 'leaderstats'
	savevalue.Parent = leaderstats
	savevalue.Name = 'Money'
	
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Money.Value}
DSService:SetAsync(uniquekey, Savetable)	

end)

Money Deduction Script (Not Saving)

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.HomePurchase:FireServer()
	print("Fired")
	if game.Players.LocalPlayer.leaderstats.Money.Value > script.Parent.Parent.Parent.Price.Value - 1 then
		local user = game.Players.LocalPlayer
		local stats = user:findFirstChild("leaderstats")
		local cash = stats:findFirstChild("Money")
		cash.Value  = cash.Value - script.Parent.Parent.Parent.Price.Value
		script.Parent.Parent.Parent.Enabled = false
		else
		end
end)

1 answer

✓ Accepted answer

You're trying to change your cash in a localscript, with FE and without using any remotes. Use remotes to deduct the cash.

3 comments

0

Remote Event or Remote Function?

0

And cash deduction works, it just doesn't save and I am using remote events to run this.

0

You're dumb. It doesn't. It may look like from the point of view of the client but the server ignores the change. So you have to use a RemoteEvent.

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