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 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
7
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"
end

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!

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 — 10y
Ad
Log in to vote
0
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()
    print("OMGZ, WHAT WAS CHANGED")
end

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

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
0
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
hi=true
print("Yay!")
wait(2)
print(":l")
end end

script.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 — 10y