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 7 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 =^)

01local ds = game:GetService("DataStoreService"):GetDataStore("stats")
02 
03local stats = {
04    "Candy";
05}
06 
07local moneys = {
08    "Moneys";
09}
10 
11game.Players.PlayerAdded:connect(function(player)
12    local leaderstats = Instance.new("NumberValue")
13    leaderstats.Name = "leaderstats"
14    leaderstats.Parent = player
15    for i=1, #stats do
View all 34 lines...

3 answers

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

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

01local ds = game:GetService("DataStoreService")
02local cds = ds:GetDataStore("stats") -- Candy
03local mds = ds:GetDataStore("moneys") -- Money
04 
05game.Players.PlayerAdded:connect(function(player)
06    local leaderstats = Instance.new("NumberValue", player) -- parent it to player
07    leaderstats.Name = "leaderstats"
08 
09    local candy = Instance.new("NumberValue", leaderstats) -- parent it to leaderstats
10    candy.Name = "candy"
11    candy.Value = cds:GetAsync(player.UserId) or 0 -- GetAsync or set it to 0
12 
13    local money = Instance.new("NumberValue", leaderstats) -- Also parent it to leaderstats
14    moneys.Name = "moneys"
15    money.Value = mds:GetAsync(player.UserId) or 0 -- GetAsync or set it to 0
View all 27 lines...
0
That script is not tested, but pretty sure it works. thesit123 509 — 7y
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 — 7y
1
on line 15 and 24, I put money instead of moneys thesit123 509 — 7y
Ad
Log in to vote
-1
Answered by
Pejorem 164
7 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 7 years ago
Edited 7 years ago

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

On Player Entering Script: (Server Side)

01local DataStore = game:GetService("DataStoreService"):GetDataStore("CandysAndMoneys")
02 
03game.Players.PlayerAdded:connect(function(player)
04 
05    local stats = Instance.new("IntValue", player)
06    stats.Name = "leaderstats"
07 
08    local candy = Instance.new("IntValue", stats)
09    candy.Name = 'Candys'
10    candy.Value = 0
11 
12    local money = Instance.new("IntValue", stats)
13    money.Name = 'Moneys'
14    money.Value = 0
15    -- delete from here if you dont want Rank
View all 39 lines...

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

On Player Leaving (Also ServerSide)

01local DataStore = game:GetService("DataStoreService"):GetDataStore("CandysAndMoneys")
02 
03game.Players.PlayerRemoving:connect(function(player)
04 
05    local key = "player-"..player.UserId   
06 
07    local valuesToSave = {player.leaderstats.Candys.Value, player.leaderstats.Moneys.Value}
08 
09    DataStore:SetAsync(key, valuesToSave)
10 
11end)

Make sure you store these in ServerScriptService.

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

Thanks, LukeGabrieI aka EnergyBrickz

Answer this question