Simple Killbrick script question

Hi, I am trying to learn lua and to do so I went into some of the premaid roblox games to look at the scripts.
In this kill brick script the code is:

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end)

My question is, where did the (hit) come from that is passing through the function? Is it just some random keyword in the library? Also why did they use

	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then

Couldn’t they have done:

script.Parent.OnTouch:connect(touch)
if touch then 
Humanoid.Health = 0

This might be a stupid question or might not even work but I don’t understand! Someone clarify please, thanks in advance.

script.Parent.Touched:Connect(function(hit)

As you can see above, hit is identified by the brick being touched. In other words, hit = the object that touched the killbrick, as identified in the function.

I’ll gave you another example:

game.Players.PlayerAdded:Connect(function(player)

“player” is the player that is joining the game, because the script is detecting when a player joins, and names the player “player”

Also, you can actually change hit to whatever you want, like this:

script.Parent.Touched:connect(function(burger)
	if burger and burger.Parent and burger.Parent:FindFirstChild("Humanoid") then
		burger.Parent.Humanoid.Health = 0
	end
end)

This script will still work even though I changed everything to “burger”. You can try it, and it will work!

So basically, “hit” was identified at the start of the function as the thing that touched your killbrick.

3 Likes

That’s a good explanation, however, you don’t need to check if burger.Parent at the same time with burger.Parent:FindFirstChild("Humanoid"), you can just use the 2nd one instead.

1 Like

When a player touches a part, the hit will become whatever part of their body that touched the part.
So if my hand touched the part, hit will be the hand.

All of the hands, legs, torso and etc are in the player’s character model. So if you want to access the player’s character from hit you would do hit.Parent

Then, they do hit.Parent:FindFirstChild("Humanoid").
:FindFirstChild() is a function that returns a child with the name of whatever you placed in the parenthasis, if one exists. If it doesn’t, it will return nil.

So, for safety and to prevent any unusual errors, they check if the hit exists, if the player’s character exists, and if they can find a Humanoid object inside the character (If a humanoid exists that means it’s a player or an NPC).
Then they set the humanoid’s health to 0, which is the player’s health. Humanoid stores properties like Health, WalkSpeed, JumpPower, MaxHealth, and etc.

I didn’t mention this, but as Doqee says:

You don’t need to check for hit.Parent since anything has a parent.

I agree. This is how I like to write a killscript:

script.Parent.Touched:Connect(function(hit)
hit.Parent:FindFirstChild:("Humanoid")
hit.Parent.Humanoid.Health = 0
end)

You mean :FindFirstChild("Humanoid")?

Oops! Yes, yes I did. Sorry! I’ll make the edit now.

Edit: Fixed it.

Functions can utilize parameters for data that will be passed in. When you declare a function, you may include one or more parameter names inside the parentheses; for example this function uses the parameter “hit”. When calling a function with parameters, specify the values that should be passed to the function. For instance, the following function takes the two numbers passed in during each call, adds them together, and outputs the result:

>  local function addNumbers(num1, num2)
> 	local result = num1 + num2
> 	print(num1 .. " + " .. num2 .. " = " .. result)
> end
>  
> addNumbers(2, 3)
> addNumbers(4, 0.5)
> addNumbers(1000, 500)

This particular function in question is an event triggered function, which does what you expect, it’s run when triggered by an event. The above function was just an example of parameters. In this case, script.Parent.Touched:connect refers to the parent of the script, and when the parent part is touched it triggers the function when it gets a connection. Each object in roblox has 3 (for the most part, sometimes more) catagories of properties, in this case, “Part” has Properties, Functions, and Events. Properties are physical appearance and position, functions are the functions that can be used in conjunction with code referring to or calling the part, and events are things that are triggered when certain things happen with the part. Part has 2 Events: Touched, and onTouched. So, when the part comes in contact with another part, the touched event is fired. The function that precedes this is now run, and the function has the parameter of “hit”.

if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")

The function contents start with an if statement. First it checks if hit (if the event was fired) then it checks if the object that touched touched the parent of the script, which is Part. If both of those are true, then it checks if the parented part was touched by an instance with the given name “Humanoid”. If so, it returns the first child of the instance of the given name “Humanoid”. If all 3 of those are true, it then sets the health of the humanoid whose parent collided with the script’s parent to 0.

Now, onto your next question of why they didn’t use

script.Parent.OnTouch:connect(touch)
if touch then 
Humanoid.health = 0

There are a few issues with this code.

  1. There is no such event called OnTouch inherited from BasePart.
  2. There is no end/semicolon to justify the end of the statement.
  3. “touch” is in parentheses, even though you didn’t call a function

Now, because you are new to Lua and don’t fully understand all the syntax and stuff, I’ve fixed those few rookie mistakes for you. It can be hard to remember at first but you’ll get it.

script.Parent.OnTouch:connect(function(touch)

if touch then Humanoid.Health = 0 end

end)

Now that we’ve fixed those minor mistakes, you are left with 2 warnings.
Annotation 2020-08-14 175057

Humanoid is unknown because it is undefined, and simply defining it as a variable won’t work either. If you just defined it as a variable, it cannot have health properties. The “.” in between Humanoid.Health signifies an distinguishment in inheritance, so anything that comes after the period is a child of the instance that precedes the period. Since Humanoid is not defined and has no parent, a warning occurs and the code is for the most part useless. If you were to clarify that the humanoid was linked up with a player, anything that the part collided with would kill the player by doing touch.Parent:FindFirstChild("Humanoid") after the then, nothing would happen still because the computer doesn’t know if it hit the part.

One major characteristic you need to remember about coding is that computers are only as smart as you want them to be, and need to be told certain instructions step by step is a certain way. It’s kinda hard to comprehend for people new to programming, because you never think logically that you have to spoon feed instructions a certain way to a machine that has processing speeds and reaction times faster than 10 people.

I hope this helped you out some, I tried to explain as least as possible without having to go into the technical details all while making sense at the same time. So if there are any logical holes in my explanation please feel free to ask any questions

1 Like

Hit is the part that touched the thing you specified

(If you did part.Touched then the thing that touched that part will be hit)

When you do if hit and hit.Parent (which shouldn’t really be there unless its a loop) you are checking for if the part and its parent exists (if the part exists then logically its parent exists)

When you check hit.Parent:FindFirstChild(“Humanoid”) you are doing this:

Lets say the right foot touched a part, this will be what you are doing.

Character
hit.Parent:FindFirstChild(“Humanoid”)

Since the humanoid is the child of the character in the games then you are checking if the parent of the object has a humanoid on it, basically checking if you can damage that thing.

Also using FindFirstChild() will return nil if it cant find, if it can find then it will return the object you searched, in this case the humanoid.

Ok so a summary here:

local part = script.Parent
part.Touched:Connect(function(hit) -- Touched event has the object that touched the specified object as parameter
         if hit.Parent:FindFirstChild("Humanoid") then -- Checks if the parent of the object that hit the part has a humanoid
                hit.Parent.Humanoid.Health = 0 -- Kills
         end
end)

-- Note that if there are npcs then they will die by them too.

Nearly forgot to say but events most likely returns a parameter so you can use

Some examples:

game.Players.PlayerAdded:Connect(function(player)
        -- player is the player that just joined.
        player.CharacterAdded:Connect(function(character)
               -- Everytime the player respawns or spawns for the first time, this will fire
               -- character is the character of the player
        end)
end)

local runServ = game:GetService("RunService")
runServ.Heartbeat:Connect(function(timeBetweenHeartbeats)
        print(timeBetweenHeartbeats) -- Prints the ammount of time passed during each heartbeat.
end)

There are lots of them, you can check at object browser or at the developer hub.