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

My leaderboard wont autosave my cash and gems?

Asked by 5 years ago

Here is my script

CashData = game:GetService("DataStoreService"):GetDataStore("CashData")
GemsData = game:GetService("DataStoreService"):GetDataStore("GemsData")

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('IntValue', plr)
    stats.Name = 'leaderstats'
    local cash = Instance.new('IntValue', stats)
    cash.Name = 'Cash'
    cash.Value = 0

    stats.Name = 'leaderstats'
    local cash = Instance.new('IntValue', stats)
    cash.Name = 'Gems'
    cash.Value = 0
end)


I tried to make it autosave but it wont work, i tried two diffrent ways but this is all ive got

1 answer

Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
5 years ago

For first remember: You not need to get few datastores, cuz you can save multiple data in one datastore.

local DS = game:GetService("DataStoreService")
local StatsData = DS:GetDataStore("StatsData")

local prefix = "User_"

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

    local Gems = Instance.new("IntValue")
    Gems.Parent = Folder
    Gems.Name = "Gems"
    local Cash = Instance.new("IntValue")
    Cash.Parent = Folder
    Cash.Name = "Cash"

    local plrData = StatsData:GetAsync(prefix..player.UserId)

    if plrData ~= nil then --checking if player have data
        Gems.Value = plrData[1] --importing data
        Cash.Value = plrData[2]
    else -- saving data if player didnt have data
        StatsData:SetAsync(prefix..player.UserId, {
            player.leaderstats.Gems.Value; 
            player.leaderstats.Cash.Value;
        })
    end
end)

game.Players.PlayerRemoving:Connect(function(player) --saves when player leave
    StatsData:SetAsync(prefix..player.UserId, {
        player.leaderstats.Gems.Value;
        player.leaderstats.Cash.Value;
    })
end)
0
you can use a numeric for loop for lines 21 and 22, and you dont need to check if :GetAsync() returns nil or not User#23365 30 — 5y
0
also he can put the values inside separate datastores if he wants User#23365 30 — 5y
0
I think multiple DataStores saving at once counts as more “saves”, and gets you needlessly closer to the save cap, right? DinozCreates 1070 — 5y
0
Yea. HaveASip 494 — 5y
0
@ EXpodo1234ALT separate datastores - bad idea, cuz why if you can put many data in one datastore? HaveASip 494 — 5y
Ad

Answer this question