Newbie scripting question

I’m extremely new to scripting and was recently following a basic tutorial of how to make a kill block on the Roblox Developer website. I was thinking of just passing over the parts I didn’t understand but I really want to get to understand Lua as a language. Within the script, I’m telling it to fetch the humanoid part that’s touching the brick, defined as ‘otherpart’. My question is, how does the script know what ‘otherpart’ is? The instructions of the code don’t define it anywhere in the script, and it doesn’t seem to be a parameter already understood by the script unless I am mistaken. Could anybody explain to me how this works?

2 Likes

Make a folder and add every kill part then add this script inside server script services

	for _, killParts in pairs(--loction of the folder:GetChildren()) do
		killParts.Touched:Connect(function(Hit)
			if Hit and Hit.Parent:FindFirstChild("Humanoid") then
				Hit.Parent.Humanoid.Health = 0
			end
		end)
end

So, the way a touched event works is that when the “part” is touched it will connect to a function. You also do not need to create a function if you don’t have anything neccesary. Touched has a parameter which detects what the part is that it touched. If the player’s right leg touched the part and you do

lava.Touched:Connect(function(Hit)
      print(Hit.Name)
end)

then you would get “RightLeg” in the output.
Now, if you want to kill a player then you would check if the part’s PARENT is a player.

lava.Touched:Connect(function(Hit)
      if game.Players:GetPlayerFromCharacter(Hit.Parent) then
            Hit.Parent:FindFirstChild("Humanoid").Health = 0
      end
end)

Setting the health of the Humanoid object of the player’s character will kill them. GetPlayerFromCharacter() is used to see if a player exists and that is a player’s body. FindFirstChild sees if the body has a humanoid object.

2 Likes

So when using .touched, any word put as the parameter will output the part thats hit?

Yes, it only uses one parameter and it is like a variable. Hit is what most developers use as the name, but it can be anything. Hit is the object that is touching the part.

1 Like

Ahhh okay, I understand, thank you!