Checking players level

I have this overhead gui which shows the players level, I want the textcolor of the players level to change depending on the players level.

This is what I have so far:

local Colours = {
	{1, Color3.fromRGB(255, 0, 0)},
	{100, Color3.fromRGB(255, 150, 0)},
	{200, Color3.fromRGB(255, 255, 0)},
	{300, Color3.fromRGB(0, 255, 0)},
	{500, Color3.fromRGB(0, 0, 255)},
	{700, Color3.fromRGB(125, 0, 255)},
	
}
table.sort(Colours, function(a, b) -- Puts table in order
	return a[1] < b[1] 
end)


for i, Colour in pairs(Colours) do		
	if PlayerLevel <= Colour[1] and PlayerLevel > Colours[i + 1][1] then
		Level.TextColor3 = Colour[2]
		wait(1)
	end
end

It gives me this error:
attempt to index nil with number

I dont know what is wrong and if this is the correct way to do it.

For the table.sort function, you don’t need the a[1], you can just do a. Same with b.

That isnt the problem

This is:

Maybe you don’t need the second bracket?

Try this:

to this: Colours[i+1]

I would assume it would be this part:

On the last iteration, I will be 6, so I + 1 will be 7, and there is no 7th value in the table.

Maybe try changing it to this:

if PlayerLevel <= Colour[1]  and PlayerLevel > Colours[i + 1][1]  or I == 6 then

I think that will still error since you are trying to index a nil which is Colours[7] to [1], this is the fixed version

if PlayerLevel <= Colour[1] and (i == #Colours or  PlayerLevel > Colours[i + 1][1])

and i also do not understand why would you want to check if the players level is less than the first Colour but greater than the second

1 Like

When I print(Colour[1]) after that if statement it prints 700.

Also is there a better way to achieve what I am trying to do?

what I THINK you are trying to do is check if player is within a level range like level 1 - 99, 100 - 200.
so i think this will do it. Replace the if statement in the for loop with this

if PlayerLevel >= Colour[1] and (i == #Colours or  PlayerLevel < Colours[i + 1][1])

What this will do is check if the PlayerLevel is greater than or equal to the level of the Colour, and checks if the PlayerLevel is less than the level of the next colour.

I added this, because let’s say you are already at the last colour, you will check if the PlayerLevel is greater than that but lesser than the next Colour, however there is no next colour so we will check if that is the last/highest color. This part should be before

because, it will check First if it is the last colour, before checking the next colour or else it will error

Thanks when I was typing this out I put <= instead of >= and > instead of <.