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

More than one leaderstat?

Asked by 6 years ago

So i'm trying to change this datastore leaderboard so it will have 2 leaderstats, which is candy and moneys, it works, however, it does not save the moneys and every time you rejoin you will have 0 moneys. I need some help with this please. Thanks =^)

local ds = game:GetService("DataStoreService"):GetDataStore("stats")

local stats = {
    "Candy";
}

local moneys = {
    "Moneys";
}

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("NumberValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    for i=1, #stats do
        local stat = Instance.new("NumberValue")
        stat.Parent = leaderstats
        stat.Value = 0
        stat.Name = stats[i]
        local stat2 = Instance.new("NumberValue")
        stat2.Parent = leaderstats
        stat2.Value = 0
        stat2.Name = moneys[i]
    end
    for i, v in pairs(player.leaderstats:GetChildren()) do
        v.Value = ds:GetAsync(player.userId..v.Name)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    for i, v in pairs(player.leaderstats:GetChildren()) do
        v.Value = ds:SetAsync(player.userId..v.Name, v.Value)
    end
end)

3 answers

Log in to vote
1
Answered by
thesit123 509 Moderation Voter
6 years ago

I save datastore manually, so this is what I have:

local ds = game:GetService("DataStoreService")
local cds = ds:GetDataStore("stats") -- Candy
local mds = ds:GetDataStore("moneys") -- Money

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("NumberValue", player) -- parent it to player
    leaderstats.Name = "leaderstats"

    local candy = Instance.new("NumberValue", leaderstats) -- parent it to leaderstats
    candy.Name = "candy"
    candy.Value = cds:GetAsync(player.UserId) or 0 -- GetAsync or set it to 0

    local money = Instance.new("NumberValue", leaderstats) -- Also parent it to leaderstats
    moneys.Name = "moneys"
    money.Value = mds:GetAsync(player.UserId) or 0 -- GetAsync or set it to 0
end)

game.Players.PlayerRemoving:connect(function(player)
    if string.sub(player.Name, 1, 5) ~= "Guest" then -- Make sure player is not a Guest
         local playerId = player.UserId -- Get player userId
         local leaderstats = player:FindFirstChild("leaderstats)
         local money = leaderstats:FindFirstChild("moneys")
         local candy = leaderstats:FindFirstChild("candy")
         mds.SetAync(playerId, money.Value ) -- Save candy
         cds.SetAync(playerId, candy.Value ) -- Save money
    end
end)
0
That script is not tested, but pretty sure it works. thesit123 509 — 6y
0
It doesn't work, unfortunately. I did a bit of tweaking to fix some errors, where you left out " with leaderstats and such, however, it won't save the moneys value. Skepticlemon 24 — 6y
1
on line 15 and 24, I put money instead of moneys thesit123 509 — 6y
Ad
Log in to vote
-1
Answered by
Pejorem 164
6 years ago

It probably doesn't work because the server closes before it saves. Do some research into BindToClose()

0
>downvotes my answer because he doesn't like it. Pejorem 164 — 6y
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

The power of YouTube and all it's videos, lol.

On Player Entering Script: (Server Side)

local DataStore = game:GetService("DataStoreService"):GetDataStore("CandysAndMoneys")

game.Players.PlayerAdded:connect(function(player)

    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local candy = Instance.new("IntValue", stats)
    candy.Name = 'Candys'
    candy.Value = 0

    local money = Instance.new("IntValue", stats)
    money.Name = 'Moneys'
    money.Value = 0
    -- delete from here if you dont want Rank
    local grouprank = Instance.new("StringValue", stats) --optional
    grouprank.Name = "Rank"
    local rank = player:GetRoleInGroup(3164314) -- Insert the group ID for whatever group you want ranks displayed for here. 

    if rank ~= 0 then
         grouprank.Value = rank
    else
         grouprank.Value = "Guest"
    end
    -- delete here if you dont want Rank
    local key = "player-"..player.UserId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        --Save format: (candy, money)
        candy.Value = savedValues [1]
        money.Value = savedValues [2]   
    else
        local valuesToSave = {candy.Value, money.Value}
        DataStore:SetAsync(key, valuesToSave)
    end

end)

--This will make it so that it'll find any existing and new players.

On Player Leaving (Also ServerSide)

local DataStore = game:GetService("DataStoreService"):GetDataStore("CandysAndMoneys")

game.Players.PlayerRemoving:connect(function(player)

    local key = "player-"..player.UserId    

    local valuesToSave = {player.leaderstats.Candys.Value, player.leaderstats.Moneys.Value}

    DataStore:SetAsync(key, valuesToSave)

end)

Make sure you store these in ServerScriptService.

If this doesn't work, please contact me (:

Thanks, LukeGabrieI aka EnergyBrickz

Answer this question