That is because, not only players can touch the brick, but bricks can touch bricks.
If you want to change a property of the humanoid of a player when they touch, you first gotta check if the thing that touched the brick really is a player.
brick = script.Parent
function onTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = 0
end
end
brick.Touched:Connect(onTouched)
Lets decompile the script a little.
The function onTouched runs everytime something touches the brick.
That means if the brick touches the ground it will run the code inside the function.
All player characters in roblox have a humanoid. "hit" is the object that hit the brick.
If the character of a player were to touch the brick, you would do hit.Parent to get to the character because either the legs, arms, torso, head, cosmetics etc. has to touch the brick.
And those objects are a child of the character.
So if my leg touches the brick, we would find the parent of the leg, which is the character.
Next that comes up is FindFirstChild.
When you do FindFirstChild, you're trying to find a child of a object that is named what you put inside the brackets. As in the example, FindFirstChild("Humanoid") will try to find an object named "Humanoid".
If FindFirstChild("Humanoid") is not successful of finding an object named "Humanoid", the it will return nil, meaning it is "nothing".
The if statement checks if there is something that is called "Humanoid" inside of the parent of the part.
If there is nothing called "Humanoid", in other words, if its nil, it will not run the code that is inside that if statement, but run past it.
If there is something named "Humanoid", then it will run the code inside the if statement.
So why does the other scripts error?
Thats because you're trying to change the property of an object that does not exist.
As mentioned before, the if statement skips the code inside of it if the object is nil, but if you jump right to hit.Parent:FindFirstChild("Humanoid").Health = 0, then that means you're not checking if the object is existing.
Say the baseplate touches your brick and you write:
hit.Parent:FindFirstChild("Humanoid").Health = 0
Okay, first baseplate, then we go up to workspace and then we try to find an object called "Humanoid" in workspace. Oh, wait! Humanoid is not existing! But why is the player trying to change the health of "Nil"? He should have checked if the object existed before changing the value! And thats when the game complains.
It Says: "I can't change health of something that does not exist!"
I hope this made you undestand a little better why you'll require those extra lines of code.
Please call me out if I'm totally wrong of something, if there is something I should add, or if you got more questions.
I guess that's all I have to say.
Happy scripting!
Waterfox
3 comments
Hit could be a different object than a part of a character, like a part of a model.
it checks if a humanoid exists before running code that requires the humanoid to exist, hence avoiding an error
But my question is, why does the other code that DOESN'T check if a humanoid exist work (while giving you an error)?