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

Could someone write up a detailed-ish paragraph or so explaining the :connect function?

Asked by 7 years ago

(How to use it, when and when not to, what can it do, ect)

Sorry for the broad 'question', Im not expecting many answers on this one, but I'd appreciate it. It doesnt have to be aimed at a complete beginner. I've been reading lua for 5 years now, and I've worked with other language like ruby and java. I dont know a lot about all these lua functions, especially all the roblox specific ones, much less how to apply them.

Thanks in advance.

1 answer

Log in to vote
3
Answered by 7 years ago

Event Connections

The :Connect function allows you to execute a certain block of code when an event happens. More specifically, it is a method of the Roblox Event object that takes a function as an argument, passes any relevant values from the event to the function, and runs the code in the function.

Example

Let's say I wanted to print out value of a Roblox NumberValue every time it changed.

local value= workspace.NumberValue

value.Changed:Connect(function(valueNew)
    print(valueNew)
end)

The code above passes an anonymous function that has one parameter to the :Connect method as an event handler (a function to be run when the event happens). The .Changed event of Roblox NumberValues passes the new value as the first parameter of the event handler whenever the .Value property of that specific Number Value changes.

Usage

You should have an idea where to use events now - anytime you need something to happen when some Roblox object is changed, you can use an event connection.

If you look through the documentation, you should see that many Roblox objects have events - these all can be given event handlers through the :Connect method. The specifics of what the event is and what values it passes to the event handler should be described there as well.

Ad

Answer this question