I've created a script that adds - or is supposed to add - a value to a players' gold count every five seconds. I've tried print debugging on certain parts, but I can't seem to find any errors. Where have I gone wrong?
Script, located in Workspace:
game.Players.PlayerAdded:connect(function(player) leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 0 gold = Instance.new("IntValue", leaderstats) gold.Name = "Gold" gold.Value = 0 rank = Instance.new("StringValue", leaderstats) rank.Name = "Rank" rank.Value = player:GetRoleInGroup(3026186) leaderstats.Parent = player rank.Parent = leaderstats end) function getGold(P,rank) 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 else print(rank) end end while true do wait(5) P = game.Players:GetChildren() for i, v in pairs(game.Players:GetChildren()) do rank = v.leaderstats.Rank.Value getGold(v,rank) end end
Your GetGold
function doesn't modify P.leaderstats.Gold.Value
-- it only modifies the Gold
local variable.
In order to modify the IntValue, you have to somewhere explicitly set .Value
somewhere:
-- always use local variables! local Gold = P.leaderstats.Gold -- i.e., NOT .Value up above -- we need to CHANGE .Value! Gold.Value = Gold.Value + 10
Instead of using elseif
, you should consider using a dictionary:
local Earning = { ["[C] Peasant"] = 2, ["[C] Merchant"] = 15, -- etc ["[N] King"] = 100, } ......... function getGold(P, rank) local Gold = P.leaderstats.Gold if Earning[rank] then Gold.Value = Gold.Value + Earning[rank] else print("unknown rank `" .. rank .. "`") end end