Help! "attempt to compare number and boolean"

Heya! I’m trying to create a script which will display a nametag in chat if a player is below a specific ran in a group and owns a specific gamepass. I realised the issue is with line 4 and the output prints:

“attempt to compare number and boolean”

local gamepassId = 8923865
local service = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
	if not player:GetRankInGroup(4981404) >= 2 then
    	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then
	       	local tags = {
	            {
	                TagText = "👑MOVIE LAND VIP👑",
	                TagColor = Color3.fromRGB(0, 255, 255)
	            }
	        }
	        local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService)
	        local speaker = nil
	        while speaker == nil do
	            speaker = ChatService:GetSpeaker(player.Name)
	            if speaker ~= nil then break end
	            wait(0.01)
	        end
	        speaker:SetExtraData("Tags",tags)
	        speaker:SetExtraData("ChatColor",Color3.fromRGB(226, 226, 0))
		end
	end
end)

Too try fix my issue I looked on the dev wiki at :GetRankInGroup etc… but can’t seem to find the cause or how to get it working properly by modifying script.

Any help is deeply appreciated! <3

3 Likes

Have you tried replacing if not player:GetRankInGroup(4981404) >= 2 then with if player:GetRankInGroup(4981404) < 2 then?

1 Like

I believe that when you specify not
in an if then statement before any other arguments then it checks for a boolean(true or false) value, but if the value is anything else it will error so if you want to check if the player’s rank is not greater than or equal to 2 then you just check for it to be less than 2.

This is incorrect unless you’re using programming languages where there’s no concept of truthy/falsy value (usually in “strictly-typed” languages like Java or C#), but Lua is not one of them.
It will not error, it’ll implicitally convert those values to a boolean (known as truthy/falsy value)
In Lua, false and nil evaluates to false, everything else is evaluates to true.


To OP:
The problem you’re having is because of operator precedence, unlike in Python and Ruby where comparsion operator are evalulated before not, in Lua unary not is evaluated first.
In your examples it evaluates not player:GetRankInGroup(4981404) first, it can be either true or false, then it tries to compare that value to 2.

Use parenthesis to evaluate the comparsion operator first.

not (player:GetRankInGroup(4981404) >= 2)
1 Like

Isn’t it better to just do (player:GetRankInGroup(4981404) < 2) and then return.

1 Like

Oh yeah, I forgot that roblox lua or maybe just lua in general will also check numbers and only check a value that’s right after the not statement so the number was only checked. My bad I was thinking that the script auto targets boolean values after a not statement because I’ve been using different coding languages recently. I see how that fixes it as it makes the script check the output of the statement rather than the number, like an equation of 2^2+2 being 4 + 2 instead of 2^(2+2) which would be 2^4 which is waaay different.

@Jhxan has wote a solution.

Not is a bool value.

Putting not in this line

will error (why are you trying to compare boolean and number: using not (use it when operating with bools) and number that is basically returned by player:GetRankInGroup(4981404)).

Let’s say that the operation you did would not error. Even then, doing if not player:GetRankInGroup(4981404) >= 2 then is pointless. This:

will get everything done.

2 Likes