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

7 comments
Thank you, that definitely clears things up!
So was I creating a new local variable with the value of the initial Gold IntValue? And in your method you were stating the location of the variable, then declaring its new value?
That's exactly right. You made the variable Gold, set it to the old value, modified it, ... and then threw it away. The correct way says "this object holds a .Value, here's the new .Value for that object."
Awesome, thank you so much for explaining that. I love learning things like this :)
After I've set up a dictionary, would it be appropriate to run an infinite loop (every 60 seconds) calling the getGold function as long as I have the P and rank variables defined before loop-calling the function?
Yea, that makes sense.
Great!