Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Money Datastore Not Saving Value Changes When A Player Leaves?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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

0
Remote Event or Remote Function? BunnyFilms1 297 — 6y
0
And cash deduction works, it just doesn't save and I am using remote events to run this. BunnyFilms1 297 — 6y
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. F4ULT1NTH3D4T4 226 — 6y
Ad

Answer this question