Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Leaderboard script failing to increase a value while yielding no errors?

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

1 answer

✓ Accepted answer

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

7 comments

0

Thank you, that definitely clears things up!

1

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?

1

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."

0

Awesome, thank you so much for explaining that. I love learning things like this :)

0

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?

0

Yea, that makes sense.

0

Great!

This discussion is preserved as it was. New answers, comments, and votes are closed.