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

How do you use / what are the differences of Event's?

Asked by 6 years ago

Hello, I am kinda new to scripting, I wanted to know what are the basic function of Events and how to use them.

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

Events are fired when something takes place in a game. They are often connected to a function which is activated when the event occurs, allowing you to run code when the event happens.

For example, the following script connects the function with the name onTouched to the Touched event. Because of this, whenever the Touched event is fired, the code within the onTouched function is executed. (In this case, the touched event is fired when the part that the script is inside of is touched)

function onTouched()
    print("Part has been touched")
end
script.Parent.Touched:Connect(onTouched)

As you can see, the :Connect() allows you to connect the event to the function given inside the brackets.

As well as this, events can provide the function with different arguments relating to whatever the event is. In the case of the Touched event, the given argument points to the other part that touched it. In the following example, the variable hit is used as this argument.

function onTouched(hit)
    print("Part has been touched by " .. hit.Name)
end
script.Parent.Touched:Connect(onTouched)

In this example, if it was a character that touched the part, hit would be the bodypart of that character that touched it. So if a player walked over the part and touched it with their Right Leg, the function would print

Part has been touched by Right Leg

There are many different types of events from many different instances, and they all provide different arguments (or no arguments on some occasions) when fired. The wiki and object browser are good places to view possible events.

Hope this helps!

Ad

Answer this question