"Why Did 'IsA' Work for Checking Humanoid, but Not '='

I was trying to create a script that detects when a part is touched by a Humanoid, and after the part is touched, it sets the Humanoid’s Health to 0. This is the script I created:

local part = script.Parent 
 
local function onTouch(otherPart)
    local Human = otherPart.Parent:FindFirstChild("Humanoid")
    if Human = Humanoid then
        Human.Health = 0
    end
end
 
part.Touched:Connect(onTouch)

Unfortunately, this script did not work and didn’t do anything at all. However, I noticed if I replaced the “if Human = Humanoid” with “if HumanIsA:(“Humanoid”)” Then it would work just fine. Why is it that by using the = signs to make a condition for it, it would do nothing but if I used that other method then it starts to work as intended? Thanks in advance.

1 Like

In Lua we have the = and == operator. The = operator is used for assignment, while the == operator is used to check for equality. In your case, you should be using == in your if-statement.

Additionally, Lua does not know what Humanoid is. To Lua, it just thinks its a variable with no value. You need to wrap it in quotes to make it a string, similarly to what you are doing on the line above. Furthermore, the string property of instances that store their class-name is called ClassName.

All in all, you would need to do if Human.ClassName == "Humanoid" then for your if-statement.

1 Like

Appreciate the feedback. For some reason if I just used the ‘==’ like you suggested, it would not work at all. But if I used Human.ClassName == Humanoid, it would work, even without putting quotes around the Humanoid part. Why is it that it still can’t work even with something like “if Human == Humanoid”, even with including the quotes around the Humanoid part in that line?

1 Like

As @OptimisticSide previously mentioned, “Humanoid” in itself is just a variable. A container that doesn’t lead to the ACTUAL instance of a Humanoid. So, the reason why Human == Humanoid does not work is because you’re comparing it to an empty variable, not the actual humanoid instance.

The reason why it doesn’t work with quotes either is because you’re comparing Human with a string (a text), not an instance.

The reason why ClassName works is that ClassName has a defined list of Instances (such as Humanoid), where if you compare the ClassName to a Humanoid (AS A STRING, because you’re not comparing ClassName to a variable), it will go through it’s predefined list of instances until it finds “Humanoid”.

I hope I worded this correctly, if there’s a better way to explain this, please let me know! I hope this helps.

1 Like

Wait I think I understand. I think it is because when I’m comparing it to “Humanoid” I am just talking about the word, Humanoid. But when I use otherPart.Parent.FindFirstChild:(“Humanoid”) I’m not referring to just a word, but an actual object that has more properties so if I just used the plain word, it wouldn’t be referring to the class itself. Am I correct? Just need some confirmation.

2 Likes

Yeah! Humanoid itself is a variable. “Humanoid” (including the quotes) is just the text. However, using the FindFirstChild method, you’re actually FETCHING the instance (the Humanoid)!

1 Like

Wow, thank you so much! Appreciate the help a lot, I can now not have that issue bugging me for the rest of the day lol :pray::pray::pray:

1 Like

For sure man! Have a good one! I may have explained it a bit poorly, hopefully not haha but I hope it brought clarification!

1 Like

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