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

Explanation of ":connect" please? [closed]

Asked by 11 years ago

Could someone explain to me what ":connect" is? Scripting tutorial I found doesn't really explain it too much at all.

Locked by BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
7
Answered by
duckwit 1404 Moderation Voter
11 years ago

ROBLOX is an event-driven scripting environment. This means that it is possible to create new events and setup scripts to listen for other events - ROBLOX itself has a multitude of built-in events for each of it's Instance types and Services (Like Part, game.Players, Humanoid, etc...)

Imagine that on your desk is a lever that you can pull and it activates a function in your code, and you want to activate this function whenever a man jogs past your house. You put a guy outside your house who shouts "Go for it!" every time a man jogs past your house, whenever you hear him say that, you pull the lever. The man who shouts "Go for it!" is doing what the :connect() method does for events and functions!

When you have an Event on an object, you use the connect() function to tell it "pull this lever when the event fires".

Take, for example, the commonly used Touched event on a Part:

1part = workspace.SomePart --A part in the workspace
2 
3function FireWhenTouched(thingThatTouched)
4    --We could name this function whatever we wanted, it is separate from the event "Touched"
5end
6 
7part.Touched:connect(FireWhenTouched) --The name of the function goes into the connector, this is like telling the man outside your house to yell "Go for it!" when the brick gets touched.

If you haven any further questions just let me know, I understand that sometimes these ideas can be hard to grasp - I've tried to make it as simple as possible!

0
No more further questions. Best explanation for me. Simple and understandable... and awesome? :3 Thank you for your time and remember "stay Frosty" :) sFrosty04 30 — 11y
Ad
Log in to vote
0
Answered by 11 years ago

The 'connect' method connects the function to the event, so that when the event happens, the function will perform. Examples -

01function onChanged()
02    print("OMGZ, WHAT WAS CHANGED")
03end
04 
05function sushis()
06    print("The function name is irrelevant")
07end
08 
09game.Players.PlayerAdded:connect(sushis) --will perform the function 'sushis' when a player is added
10game.Workspace.Part.Changed:connect(onChanged) --will perform the function 'onChanged' when a part property is changed

If you were to just write a function, without a connection line, the function won't perform.

Log in to vote
0
Answered by 11 years ago

The ':connect' is for connecting a function to the Part, Model, or game, here is a example(Sorry if I mistaken typed multiple errors in logic and/or scripting)

01local hi=false
02 
03function Lolzor() --The function
04if hi==false then
05hi=true
06print("Yay!")
07wait(2)
08print(":l")
09end end
10 
11script.Parent.Touched:connect(Lolzor) --Connects to the function, if part is touched, it will react and activate the function
0
You should be careful with how you word any programming-related description, one does not connect a function to an Instance (such as Part, Model, or game) but rather to EVENTS belonging to those entities. duckwit 1404 — 11y