Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Confused about events?

Asked by 10 years ago

script.Parent.Touched:connect(function(part)

--What does part mean?

3 answers

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

'part' is the parameter or argument of the function. It is whatever the event is triggered by.

For example, anything that triggered the 'Touched' event will be the 'part' argument.

  • If a brick named "Block" touched the Part with the script, "Block" will be the argument.

If you used the argument in your function, it will do whatever you want it to do to the argument ("Block" in the above example).

script.Parent.Touched:connect(function (arg)        -- The argument can be anything; it's a variable.
arg.Transparency = 1        -- Will make anything that touches the brick invisible
end)
script.Parent.Touched:connect(function (asdfadsf)
asdfadsf.BrickColor = BrickColor.new("Really red")      -- Will change the argument's color to really red
end)
Ad
Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Parts have a Touched event attached to them.

An event is an object describing some future action that you can attach code to.

Events have a :connect method (a function that you can call on it, just like :Remove() or :Destroy() or :FindFirstChild()).

You give the :connect method a function as its parameter.


A bit simple syntax looks like this:

function myfunc(other)
    print(other, "touched me!")
end

script.Parent.Touched:connect( myfunc )

We're telling Touched to fire the function we gave whenever the event happens. Since we gave it myfunc, that means myfunc gets called whenever something touches script.Parent.

It passes 1 parameter to that function, which is the part that is touching it (called other in the definition of myfunc)

0
This helps so much thank you! BenaBurrito 5 — 10y
Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
10 years ago

"Part" is the part that the "Parent" touches. Say the parent is called A, and the part that gets touched is called B. Whenever A touches any part, it'll set the part as a variable called B. This is an example script:

script.Parent.Touched:connect(function(B)
print(B.Name) --If the part that the "Parent" touches is named "Part", then the game will print "Part".
end) --Remember to add the closing parentheses to let the function run correctly.

Hope this helps. If you have any questions or problems, please leave a comment below. :P

0
So can i make B a specific thing by saying B = BenaBurrito 5 — 10y
0
Oh no I cant, script.parent is the part that is being touched. BenaBurrito 5 — 10y

Answer this question