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 =^)
01 | local ds = game:GetService( "DataStoreService" ):GetDataStore( "stats" ) |
02 |
03 | local stats = { |
04 | "Candy" ; |
05 | } |
06 |
07 | local moneys = { |
08 | "Moneys" ; |
09 | } |
10 |
11 | game.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 |
I save datastore manually, so this is what I have:
01 | local ds = game:GetService( "DataStoreService" ) |
02 | local cds = ds:GetDataStore( "stats" ) -- Candy |
03 | local mds = ds:GetDataStore( "moneys" ) -- Money |
04 |
05 | game.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 |
It probably doesn't work because the server closes before it saves. Do some research into BindToClose()
The power of YouTube and all it's videos, lol.
On Player Entering Script: (Server Side)
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "CandysAndMoneys" ) |
02 |
03 | game.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 |
--This will make it so that it'll find any existing and new players.
On Player Leaving (Also ServerSide)
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "CandysAndMoneys" ) |
02 |
03 | game.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 |
11 | end ) |
Make sure you store these in ServerScriptService.
If this doesn't work, please contact me (:
Thanks, LukeGabrieI aka EnergyBrickz