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

How to set a sum of money in this DataStore script ?

Asked by 4 years ago

Hello everyone, So, I copied code from DataStore and I would like to define a sum of money but my tests don't work, I don't know how to do it.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(Player)
        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = Player

        local cash = Instance.new("IntValue")
        cash.Name = "Cash"
        cash.Parent = leaderstats

        local data
        local success, errormessage = pcall(function()
            data = myDataStore:GetAsync(Player.UserId.."-cash")
        end)
        if success then
            cash.Value = data
        else
            print("There was an error whilst getting your data")
            warn(errormessage)
        end
end)

game.Players.PlayerRemoving:Connect(function(Player)
        local success, errormessage = pcall(function()
            myDataStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value)
        end)

        if success then
            print("Data successfully saved")
        else
            print("There was an error when saving data")
            warn(errormessage)
        end

end)

I tryed:

        local cash = Instance.new("IntValue")
        cash.Name = "Cash"
        cash.Parent = leaderstats
        cash.Value = 1000

But it doesn't work, help me please

1 answer

Log in to vote
0
Answered by
BryanFehr 133
4 years ago

Hello, if by "define" you mean set a specific amount of currency to the player when you join the game, so for example, the player starts with a certain amount, add this to line 12 of your script;

cash.Value = 1000

If you don't mean this, and mean that you wish to test this datastore, then when you "run" or "server test" the game, open the Menu>Settings>(Scroll to the Bottom)Dev Consol>Server And on the "Command Line" at the bottom type the following line;

game.Players.USERNAME.cash.Value = NUM

Where USERNAME is your ROBLOX Username, and NUM is the amount.

Please comment below if you have any questions ,or if I didn't answer what you wished!

0
Thank you for answering me and yes, I want when a Player enters the game, he has a certain amount of money so as you said I put cash.Value = 1000 But it doesn't work. Altair77100 6 — 4y
0
Try this: Making line 12 line 13, and putting "cash.Value = 1000" to line 11! BryanFehr 133 — 4y
0
@BryanFehr It doesn't work, I don't understrand why it doesn't work Altair77100 6 — 4y
0
It may be due to fact that you have already saved 0 cash in datastore that gets overwritten. Change datastore name and test again karlo_tr10 1233 — 4y
View all comments (2 more)
0
@karlo_tr10 I changed datastore name in line 2: local myDataStore = DataStoreService:GetDataStore("myDataStore"), but it doesn't work Altair77100 6 — 4y
0
Finally, I changed my script and it's work but thank you for you help Altair77100 6 — 4y
Ad

Answer this question