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

script.Parent.Touched:connect(function(part) What does it means?

Asked by 8 years ago
script.Parent.Touched:connect(function(part)

can anyone explain what does the 'function and part' in the brackets mean

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Touched is an event. This means that if you attach (or "connect") it to a function, it will run that function every time the event "fires", or gets activated.

Every object has a certain set of events. For Part objects, they do things like detect when another part bumps up against it (the Touched event).

A more common way to see your code is this:

local function MyFunction()
    print("The function is run!")
end

script.Parent.Touched:connect(MyFunction)

However, two things are different:

1) When an event fires its connected function, sometimes it can send that function certain information about how the event happened. With the Touched event, for example, it runs the function while sending the other Part that touched script.Parent, in this case, or whatever Part the event is connected to.

This is accomplished with arguments in functions, which look like this:

local function MyFunction(partThatTouched)
    print("The function is run! The name of the part that ran it is " .. partThatTouched.Name)
end

script.Parent.Touched:connect(MyFunction)

That will print out the name of the part that touched script.Parent.

2) Your code doesn't have a name for the function. It just defines it directly inside the parentheses following connect. This is also allowed, but instead of using the function name, you just put function(argument1, argument2), along with the definition for the function and its associated end, inside them:

script.Parent.Touched:connect(
    function(partThatTouched)
        print("The function is run! The name of the part that ran it is " .. partThatTouched.Name)
    end
)

That code will do the same thing as the code before it. The only difference is that I didn't have to name the function and both created and connected it in the same line.

0
Oh now I get it, Thnx DrPredablox 153 — 8y
0
Thx! ruelinha 0 — 4y
Ad
Log in to vote
0
Answered by 8 years ago

it means you are starting a function. think of it like this, when you say

script.Parent.Touched:connect

You are saying that when whatever part this script is under in your workspace gets touched, you want something to happen. So, to get something to happen you first have to tell the script you want something to happen. to do this, you use the "connect" command, to tell the script we are about to tell it what to do once the part has been touched. Inside the brackets of that command we add a function,  when we use function, it also has a set of brackets. inside those brackets, we name the function. when you first start off scripting, name it something relative to what you are trying to accomplish with the script. For example, if once the part is touched you want said part to change colors, you should name it ColorChange, to make things less complicated. in your line of code you used however, it is named part. This name really has no significance to the script, you can name it whatever you want, what I said was just a suggestion to make things easier. Now, inside of the original set of brackets from the connect() command we used, we are going to tell the script what we want it to do. ill show you.

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

script.Parent.Color = Color3.new(5,0,5)

end)

We just told the script that when the part is touched, we want the color to change to the color of 5,0,5 I'm not sure what color this will be, I'm just using it as an example. If you don't know what Color3 is or why I added more brackets in for, its because colors are identified in lua by a set of three numbers, to change the color of the part, you have to change the numbers. so we set the parts 3 

color value numbers to 5,0,5. I'm not sure what this color is going to be, its just for demonstration. After this is done, we want to tell the script to stop what its doing. so we type end, and then close the bracket that we opened in the connect line of code. I did the best I could to explain, I hope this helps! also, if you want to learn more about scripting, Roblox has some great video guides on youtube. look up hours of code first. you wont be disappointed.

0
yeah i butchered that, listen to the other guy. lol ace12345678135 50 — 8y

Answer this question