Problem with doing math?

Hi so I have this tier system and the code needs to figure out which tier a player is, more boom power = higher tier, when running the script with 5 boom power it prints tier 9 despite the fact it should be tier 1? can anybody help me know why. I have found that the > and < signs in roblox are very bugy

function figureouttier()
	if player.leaderstats.BoomPower.Value <= 200 then
		tier = 1
	end
	
	if player.leaderstats.BoomPower.Value <= 400 then
		tier = 2
	end
	
	if player.leaderstats.BoomPower.Value <= 600 then
		tier = 3
	end
	
	if player.leaderstats.BoomPower.Value <= 800 then
		tier = 4
	end
	
	if player.leaderstats.BoomPower.Value <= 1000 then
		tier = 5
	end
	
	if player.leaderstats.BoomPower.Value <= 1200 then
		tier = 6
	end
	
	if player.leaderstats.BoomPower.Value <= 1400 then
		tier = 7
	end
	
	if player.leaderstats.BoomPower.Value <= 1600 then
		tier = 8
	end
	
	if player.leaderstats.BoomPower.Value <= 2000 then
		tier = 9
	end
	print(tier)
end
2 Likes

Put all these if statements in reverse order and it will work.

It’s happening because the Lua code is interpreted from the top to the bottom, so if BoomPower is 5 it will go through all these if statements and if every condition is satisfied and every part of code associated with that condition makes a change to the tier variable then only the last change will be visible as it will overwrite the previous changes to tier.

2 Likes

This is because it is true for all of your if statements, so it runs your code. You shouldnt be using repeated if statements for this anyway, try doing:
tier = math.floor(player.leaderstats.BoomPower.Value / 200)

you are using smaller and equal to <=, but you need to detect another thing

function figureouttier()
	if player.leaderstats.BoomPower.Value <= 200 then
		tier = 1
	end
	
	if player.leaderstats.BoomPower.Value <= 400 and > 200 then
		tier = 2
	end
	
	if player.leaderstats.BoomPower.Value <= 600 and > 400 then
		tier = 3
	end
	
	if player.leaderstats.BoomPower.Value <= 800 and > 600 then
		tier = 4
	end
	
	if player.leaderstats.BoomPower.Value <= 1000 and > 800 then
		tier = 5
	end
	
	if player.leaderstats.BoomPower.Value <= 1200 and > 1000 then
		tier = 6
	end
	
	if player.leaderstats.BoomPower.Value <= 1400 and > 1200 then
		tier = 7
	end
	
	if player.leaderstats.BoomPower.Value <= 1600 and > 1400 then
		tier = 8
	end
	
	if player.leaderstats.BoomPower.Value <= 2000 then
		tier = 9
	end
	print(tier)
end

no, thats it wrong, it’s: tier = math.ceil(player.leaderstats.BoomPower/200)

I found just changing the <= to >= works fine, Thanks.

Keep in mind that the ‘>’ and ‘<’ signs in Lua are not “very buggy”. These symbols are called inequality operators. They allow you to determine if a number is larger than another number or if a number is smaller than another one.

For example, the expression 5 > 4 will be evaluated as true. This is because five is larger than 4. On the other hand, 4 > 5 will be evaluated as false. One trick to remember which symbol means what is to think of them as alligators (as > looks like an alligators mouth when eating). The alligator always wants to eat the larger number of fish, so whatever number the mouth is open toward is the larger number.

Here is a graphic for visual learners:

1 Like

Yeah my bad, but my point still stands, OP shouldn’t be using if statements for this.

(Consider using else if, if that’s what you want out of your code.)

2 Likes