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!
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
A boolean is a true/false value. Not a number value. Make sure you're using an IntValue and not a Boolean value.
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