Attempt to compare nil <= number

SYSTEM
When a player joins, I want the players textcolor to change dependant on their rank in a group. (EXAMPLE) If you have a rank of 255, you get a violet color.
ISSUE
The color of the rank doesn’t change. Also, I’m getting an error on line 33.

lua ServerScriptService.OverheadGui:33: attempt to compare nil <= number

local groupId = 4763170 -- REPLACE 4763170 WITH YOUR GROUP ID

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		wait(1)
		if character and character:FindFirstChild("Head") then
			local guiClone = script.OverheadGui:Clone()
			guiClone.Parent = character.Head
			local informationLabel = guiClone.InformationLabel
			local playerRank = player:GetRoleInGroup(groupId)
			informationLabel.Text = player.Name .. "  " .. (playerRank and playerRank or "")
			if playerRank == 255 then
				informationLabel.TextColor3 = Color3.fromRGB(153, 50, 204) -- violet color
			else
				local rankColors = {
					[1] = Color3.fromRGB(255, 0, 0), -- red
					[2] = Color3.fromRGB(255, 153, 0), -- orange
					[3] = Color3.fromRGB(255, 255, 0), -- yellow
					[4] = Color3.fromRGB(0, 255, 0), -- green
					[5] = Color3.fromRGB(0, 0, 255), -- blue
					[6] = Color3.fromRGB(128, 0, 128), -- purple
					[7] = Color3.fromRGB(255, 255, 255), -- white
					[8] = Color3.fromRGB(128, 128, 128), -- gray
					[9] = Color3.fromRGB(210, 180, 140), -- tan
					[10] = Color3.fromRGB(0, 128, 128), -- teal
					[11] = Color3.fromRGB(255, 0, 255), -- magenta
					[12] = Color3.fromRGB(128, 0, 0), -- maroon
					[13] = Color3.fromRGB(255, 165, 0), -- gold
					[14] = Color3.fromRGB(245, 75, 75), -- brick red
					[15] = Color3.fromRGB(250, 250, 250), -- pearl
					[16] = Color3.fromRGB(0, 0, 0) -- black
				}
				local colorIndex = playerRank and tonumber(playerRank) <= 17 and tonumber(playerRank) or 16 -- if the rank is higher than 16, use black
				informationLabel.TextColor3 = rankColors[colorIndex]
			end
		end
	end)
end)

Any help would be greatly appreciated!

At the end lines it gets confusing. Don’t you want to use an if statment? And aren’t you setting colorIndex to multipile values?

You are using GetRoleInGroup; should be GetRankInGroup.

I have not tested this yet but I rewrote your script. Try this?

local groupId = 4763170 -- REPLACE 4763170 WITH YOUR GROUP ID

local rankColors = {
	[0] = Color3.fromRGB(255, 255, 255), -- white
	[1] = Color3.fromRGB(255, 0, 0), -- red
	[2] = Color3.fromRGB(255, 153, 0), -- orange
	[3] = Color3.fromRGB(255, 255, 0), -- yellow
	[4] = Color3.fromRGB(0, 255, 0), -- green
	[5] = Color3.fromRGB(0, 0, 255), -- blue
	[6] = Color3.fromRGB(128, 0, 128), -- purple
	[7] = Color3.fromRGB(255, 255, 255), -- white
	[8] = Color3.fromRGB(128, 128, 128), -- gray
	[9] = Color3.fromRGB(210, 180, 140), -- tan
	[10] = Color3.fromRGB(0, 128, 128), -- teal
	[11] = Color3.fromRGB(255, 0, 255), -- magenta
	[12] = Color3.fromRGB(128, 0, 0), -- maroon
	[13] = Color3.fromRGB(255, 165, 0), -- gold
	[14] = Color3.fromRGB(245, 75, 75), -- brick red
	[15] = Color3.fromRGB(250, 250, 250), -- pearl
	[16] = Color3.fromRGB(0, 0, 0), -- black
	[255] = Color3.fromRGB(153, 50, 204), -- violet
}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		task.wait()
		local guiClone = script.OverheadGui:Clone()
		guiClone.Parent = character.Head
		local informationLabel = guiClone.InformationLabel
		local playerRank = player:GetRankInGroup(groupId)
		local playerRole = player:GetRoleInGroup(groupId)
		informationLabel.Text = player.Name .. "  " .. (playerRole and playerRole or "")
		
		local colorIndex = rankColors[0] -- Default for non-members.
		
		if playerRank >= 16 and playerRank < 255 then
			colorIndex = rankColors[16]
		else
			colorIndex = rankColors[playerRank]
		end
		
		informationLabel.TextColor3 = colorIndex
	end)
end)
1 Like

I took the liberty of making some quick edits to your code:


-- Try to use SNAKE_CASE for constant variables.
local GROUP_ID  = 4763170 -- REPLACE 4763170 WITH YOUR GROUP ID

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		wait(1)
		if character and character:FindFirstChild("Head") then
			local guiClone = script.OverheadGui:Clone()
			guiClone.Parent = character.Head
			local informationLabel = guiClone.InformationLabel

            -- Use GetRankInGroup as it returns an integer.
            -- GetRoleInGroup will return a string with the name of the role.
			local success, result = pcall(function()
                return player:GetRankInGroup(GROUP_ID)
            end

            local playerRank = nil;
            if (not success) then
                playerRank = 0; -- assume unranked if this fails
                warn("player:GetRankInGroup() failed. Output:", result);
            else
                playerRank = result;
            end

			informationLabel.Text = player.Name .. "  " .. (playerRank and playerRank or "")
			if playerRank == 255 then
				informationLabel.TextColor3 = Color3.fromRGB(153, 50, 204) -- violet color
			else
                -- No need to use a lookup table here. This is
                -- mimicking the functionality of a normal array.
				local rankColors = {
                    Color3.fromRGB(255, 0, 0), -- red
                    Color3.fromRGB(255, 153, 0), -- orange
                    Color3.fromRGB(255, 255, 0), -- yellow
                    Color3.fromRGB(0, 255, 0), -- green
                    Color3.fromRGB(0, 0, 255), -- blue
                    Color3.fromRGB(128, 0, 128), -- purple
                    Color3.fromRGB(255, 255, 255), -- white
                    Color3.fromRGB(128, 128, 128), -- gray
                    Color3.fromRGB(210, 180, 140), -- tan
                    Color3.fromRGB(0, 128, 128), -- teal
                    Color3.fromRGB(255, 0, 255), -- magenta
                    Color3.fromRGB(128, 0, 0), -- maroon
                    Color3.fromRGB(255, 165, 0), -- gold
                    Color3.fromRGB(245, 75, 75), -- brick red
                    Color3.fromRGB(250, 250, 250), -- pearl
                    Color3.fromRGB(0, 0, 0) -- black
				}
                
                -- if rank >= 16, use black.
                local colorIndex = math.min(playerRank, 16)
				informationLabel.TextColor3 = rankColors[colorIndex]
			end
		end
	end)
end)

I put some comments in places where I made improvements. Your code had two issues:

Firstly, you were using Player:GetRoleInGroup(groupId) -> string, which returns a string of the role name for the player in the group. This is not what we wanted.

Secondly, and what ultimately caused the error you posted, was not handling an API call correctly. Player:GetRankInGroup(groupId) -> int will return an integer of the rank number for the role the player is assigned to in the group, but there is a catch: Get*InGroup will initially make an API call to ROBLOX’s group API endpoint to retrieve the relevant data and any calls thereafter will be returned from a cache.

Because of this behaviour, our responsibility as developers is to write code which responds appropriately to any errors caused on behalf of the initial request failing unexpectedly, or suffer at the hand of undefined behaviour which stops our script from functioning correctly.

To do this, we use a pcall to catch any error and handle it appropriately. See the lua reference manual for more information on exception handling techniques.

Apart from that, I also changed the wording of the groupId constant to GROUP_ID to make its use more clear to a reader and additionally converted your rank lookup table to an array, as they functioned identically.

Any questions, feel free to ask! :^)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.