While attempting to make a leaderboard script that automatically adds a certain amount of currency to a player every minute based off of their rank, I've run into a problem. While the leaderboard properly displays the users' rank, there's no value labeled "Gold". Script below, thank you for any help!
game.Players.PlayerAdded:connect(function(player) leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 0 gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 rank = Instance.new("StringValue") rank.Name = "Rank" rank.Value = player:GetRoleInGroup(3026186) leaderstats.Parent = player rank.Parent = leaderstats local function getGold(plyr,rank) p = game.Players:WaitForChild(plyr) Gold = p.leaderstats.Gold.Value if rank == "[C] Peasant" then Gold = Gold + 2 elseif rank == "[C] Merchant" then Gold = Gold + 15 elseif rank == "[C] Shopkeeper" then Gold = Gold + 20 elseif rank == "[C] Wealth Nobleman/Noblewoman" then Gold = Gold + 50 elseif rank == "[M] Enlisted" then Gold = Gold.Value + 5 elseif rank == "[M] Private" then Gold = Gold + 10 elseif rank == "[M] Corporal" then Gold = Gold + 20 elseif rank == "[M] First Sergeant" then Gold = Gold + 20 elseif rank == "[M] Staff Officer" then Gold = Gold + 200 elseif rank == "[M] 2nd Lieutenant" then Gold = Gold + 30 elseif rank == "[M] 1st Lieutenant" then Gold = Gold + 30 elseif rank == "[M] Captain" then Gold = Gold + 35 elseif rank == "[M] Field Marshal" then Gold = Gold + 35 elseif rank == "[M] High Constable" then Gold = Gold + 40 elseif rank == "[N] Knight" then Gold = Gold + 45 elseif rank == "[N] Baron/Baroness" then Gold = Gold + 45 elseif rank == "[N] Count/Countess" then Gold = Gold + 60 elseif rank == "[N] Duke/Duchess" then Gold = Gold + 70 elseif rank == "[N] King" then Gold = Gold + 100 end end while true do wait(60) getGold(player,rank.Value) end end)
The problem is that you have not set the parent of IntValue gold :-
game.Players.PlayerAdded:connect(function(player) leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 0 gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 rank = Instance.new("StringValue") rank.Name = "Rank" rank.Value = player:GetRoleInGroup(3026186) leaderstats.Parent = player rank.Parent = leaderstats -- parent for gold has not been set -- so, p.leaderstats.Gold.Value will be nil end)
You also have a function which gets the gold:-
local function getGold(plyr,rank) Gold = p.leaderstats.Gold.Value p = game.Players:WaitForChild(plyr) ...
You then add the amount to the Gold variable but you do not set the IntValues new gold value so it will not change in the leaderstats.
Other things to think about:-
game.Players.PlayerAdded:connect(function(player) ... -- this infinate loop will be ran for every player that joins the game while true do wait(60) getGold(player,rank.Value) end end)
I would also recommend that you simplify the the if statements with a table. e.g:-
local exTable = { ['Peasant'] = 10, ['Merchant'] = 20 } local function addGold(plr) -- rank arg removed as we can find it local rank = plr.leaderstats.Rank -- gets the players rank local Gold = plr.leaderstats.Gold -- we get the IntValue object not the value -- check that we have all of the data e.g. not nil if Gold and rank and exTable[rank.Value] then -- we add the gold Gold.Value = Gold.Value + exTable[rank.Value] else -- nil found end end
To update all players leaderstats we can use a nested loop (one infinite loop and a loop to update all the players gold) :-
while true do -- the infinite loop for _, plr in pairs(game.Players:GetPlayers()) -- the 'GetPlayers' function will return an array(table) of players which we then use in the for loop addGold(plr) -- adds the gold end end