local part = script.Parent
local function onPartTouched(otherPart)
-- Get the other part's parent
local partParent = otherPart.Parent
-- Look for a humanoid in the parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
-- Do something to the humanoid, like set its health to 0
humanoid.Health = 0
end
end
part.Touched:Connect(onPartTouched)
As you can see on line no.7 he used FindFirstChildWhichIsA(“Humanoid”)
but i always use FindFirstChild in this kind of situation i wonder what’s the difference
:FindFirstChildWhichIsA is used to find children that are of the class specified as a parameter. For example, in your case partParent maybe a character and the code is checking if there is a child which is of type “Humanoid” inside the character.
:FindFirstChild is used to find a child that has the name specified as a parameter between the parantheses. If you used :FindFirstChild here, even a normal part named “Humanoid” could be found.
FindFirstChild will find anything by given name, FindFirstChild(objectName). If given object name was not found, it would return nil. FindFirstChildWhichIsA is same like FindFirstChild but the difference is it required class of object. It not required object name but required object class. Example: (BasePart, UnionOperation, DataModel, Part, NegativePart). If class object was not presented with given class object. It would return nil else return the object. (If there is multiple object with same class, it choose random.).
They are both pretty self explanatory, FindFirstChild finds first child with a given name and FindFirstChildWhichIsA finds first child that is a given class
It indeed will (Humanoid in characters is usually named “Humanoid”)
BUT
That’s exactly how cheaters make themselves god if you do damage on client side - they rename humanoid name and scripts which use :FindFirstChild(“Humanoid”) simply break because Humanoid isn’t named Humanoid anymore.
That’s why you should use FindFirstChildOfClass(“Humanoid”) or FindFirstChildWhichIsA(“Humanoid”)
It’s safer if cheaters try to mess with your game or Roblox introduces some breaking change (hopefully not)
It is not about security, but rather safety (in the case of avoiding errors), but if there is an exploiter you should not bother increasing safety just for them. Remember that the server should never trust the client anyways