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

Why won't the money save when I redeem a code from the GUI?

Asked by 5 years ago

The datastore won't save the money I get from the code.

Here is the script for the datastore:

local dsService = game:GetService("DataStoreService")

local ds = dsService:GetDataStore("MoneyStats") -- Change this with a random data name!

game.Players.PlayerAdded:Connect(function(plr)

local folder = Instance.new("Folder", plr)

folder.Name = "leaderstats"

local currency = Instance.new("IntValue", folder)

currency.Name = "Money" -- Currency name.

currency.Value = ds:GetAsync(plr.UserId) or 0

currency.Changed:Connect(function()

ds:SetAsync(plr.UserId, currency.Value)

end)

end)

game.Players.PlayerRemoving:Connect(function(plr)

ds:SetAsync(plr.UserId, plr.leaderstats.Money.Value) -- Replace 'Money' with your currency name!

end)

I hope you could help me out.

Thank you!

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

What is the "code" you get the money from? Is it like a promo code which is supposed to give money once? If so, does the money save when you normally earn it? Please make your question more clear.

Also, Idk if it's related, but in your code you used incorrect " (quote) symbols aswell as incorrect - (dash) symbols, which could make it syntax error and not run. Here is it fixed:

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"

    local currency = Instance.new("IntValue", folder)
    currency.Name = "Money"
    currency.Value = ds:GetAsync(plr.UserId) or 0
    currency.Changed:Connect(function()
        ds:SetAsync(plr.UserId, currency.Value)
    end)

end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync(plr.UserId, plr.leaderstats.Money.Value)
end)

Btw, saving the player data every time the scoreboard value is changed is a bad idea due to rate limits. You should either save it only when they leave, or every x seconds.

0
I do apologize that my question was not clear, I mean by the word "code" is codes that you can find and give you money, for example like RoCitizens. Luke2323244 17 — 5y
Ad

Answer this question