Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Attempt to compare boolean and number" error... I dont understand?

Asked by 4 years ago

So I'm actually editing the ROBLOX weapons kit, however: I'm trying to give away coins when you kill a player. BUT this weird error pops up in the output:

"Attempt to compare boolean and number" at this line of code:

if not workspace[targetName].Humanoid.Health <= 0 then

And yes "targetName" is the user damaged.

Thank you for every help!

0
Try to print the workspace[targetName].Humanoid.Health and see what value it is. Soban06 410 — 4y
0
I tried that @Soban06, it prints the current health in a number. Hello1502HD 5 — 4y

3 answers

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago

operators in lua, or any programming language, really, have this thing called "operator precedence"—if you've ever heard of order of operations, PEMDAS, BODMAS, whatever, it's kind of like that but with lua operators

if you're not sure what exactly is considered an "operator", there's a full list of them and their precendences

the not operator has higher precedence over all of the comparison operators, and not gets the opposite of whether a value is truthy, so when you do not workspace[targetName].Humanoid.Health <= 0, it gets turned into false <= 0, and then the script throws an error because you obviously can't check if a boolean is less than or equal to a number

instead you have two other options

you can wrap the workspace[targetName].Humanoid.Health <= 0 part in parentheses, turning it into this:

if not (workspace[targetName].Humanoid.Health <= 0) then

or, you could just use the greater than operator

if workspace[targetName].Humanoid.Health > 0 then
0
Nice AntoninFearless 622 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

A boolean is a true/false value. Not a number value. Make sure you're using an IntValue and not a Boolean value.

Log in to vote
0
Answered by 4 years ago

This is because you are using the operator not which is used to compare the opposite of a boolean(if that makes sense), anywho, you can't use not in a situation comparing a boolean to a number. So what can you do? Here you go:

if workspace[targetName].Humanoid.Health <= 0 then
    print("Dead humanoid")
    else
    print("Not a dead humanoid")
    --code here
end

If you need an article to understand a little bit more here you go Article

Answer this question