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)

3 comments
Remote Event or Remote Function?
And cash deduction works, it just doesn't save and I am using remote events to run this.
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.