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

Explanation of ":connect" please? [closed]

Asked by 10 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
Answered by
duckwit 1404 Moderation Voter
10 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:

part = workspace.SomePart --A part in the workspace

function FireWhenTouched(thingThatTouched)
    --We could name this function whatever we wanted, it is separate from the event "Touched"

part.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!

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 — 10y
Log in to vote
Answered by 10 years ago

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

function onChanged()

function sushis()
    print("The function name is irrelevant")

game.Players.PlayerAdded:connect(sushis) --will perform the function 'sushis' when a player is added
game.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
Answered by 10 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)

local hi=false

function Lolzor() --The function
if hi==false then
end end

script.Parent.Touched:connect(Lolzor) --Connects to the function, if part is touched, it will react and activate the function
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 — 10y