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

How to use Touched()?

Asked by 10 years ago

I was just wondering how I could use the touched() function (I believe..) I need it so that when I touch a brick it would wait a certain amount of time, and then remove itself from the workspace. Anyone that can help?

2 answers

Log in to vote
0
Answered by 10 years ago

Put this in a script under the part:

script.Parent.Touched:connect(function() --fires when touched
wait(5) --waits 5 seconds then destroys brick
script.Parent:Destroy()
end)

I would recommend going to the wiki first and giving it a try yourself before coming here, it'll help you be a more independent scripter so you won't have to come to us for questions like this. The wiki really does a good job explaining the basics.

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Touched is an event.

http://wiki.roblox.com/index.php?title=Touched_(Event)


Events have a connect method. You give the connect method a function value, and when the event happens (also called fires), the function you connected is called.

Here is a simple script that may be helpful to understanding:

local me = script.Parent;

touchedFunction = function(partTouching)
    print(partTouching,"touched",me);
end

me.Touched:connect( touchedFunction );
-- me.Touched is event
-- We "connect" the function `touchedFunction` to it
-- so when the part is touched, `touchedFunction` is called.

It just prints the names of the two parts contacting (where one of them is me, script.Parent.)

Answer this question