Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What are these functions/scripts used for?

Hello, everyone! The code below kills the player when they touch it. I have some questions to make tho...

  1. What is ** function(brick) ** used for

  2. Why we do "FindFirstChild"

  3. Why we Write "brick.Parent" To Locate the player?

Thanks!

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

1 answer

✓ Accepted answer

We assume that script.Parent is a BasePart, thus it has the Touched event. Events of instances are RBXScriptSignal instances. This instance is in charge of connecting functions to events. One of the functions of the RBXScriptSignal is :Connect(function func). This will take a function as an argument and guarantee to call it when the event is omitted.

function(brick) is the beginning of the definition of the function we would like to call when the Touched event fires. We have a paramter brick in the function definition because the Touched event gives us the other part that touched the original part.

  1. We can not gaurentee that the brick that touched our script.Parent is a Character. So if we did brick.Parent.Humanoid the script would error. Using :FindFirstChild(string name) lets us check to see if something is there without erroring. If something is there we get that thing, if it is not there we get nil.

We use brick.Parent to locate the Character. Players have Characters that are models that contain up to 15 different parts that are connected together. We use brick.Parent to see if we have a Character in our case.

1 comment

0

Thank you so much, this was so nicely in detailed explained

This discussion is preserved as it was. New answers, comments, and votes are closed.