What are events?

Basic tutorial of what events are and how to use them.

by ma_t1

Author Avatar

Quick disclaimer before we begin, you need to know how functions work, but they are quite simple too, and shouldn't take you long to learn.

As for the location of the script, you could have this as the child of the part in which the event will be triggered from to keep everything looking nice, but this will work in the ServerScriptService too, just script.Parent wouldn't work.

So, what are events?

To quickly sum it up, when an event is fired, whatever you've told the script to happen will happen, but only after a certain event has fired, like you clicking on a part. Here's a quick example:

local part = script.Parent
local baseplate = game.Workspace.Baseplate

local function onTouch()
    baseplate.BrickColor = BrickColor.new("Really red")
end

part.PartTouched:Connect(onTouch)

So basically what this script would do is once you touch a part named "Part," the color of the baseplate would be changed to red.

This isn't the only way to create functions though, there is another way which takes up less space.

local part = game.Workspace.Part
local baseplate = game.Workspace.Baseplate

part.PartTouched:Connect(function()
    baseplate.BrickColor = BrickColor.new("Really red")
end)

This ends up doing the same exact thing as the one before, and you can use whichever you'd like, but you will see both of them used in other people's scripts, and it's important you know the two ways of doing things.

But what does all of that mean?

Here's a quick summary of what everything in that last line means:

part.event:Connect(name of function)

Elaborated even more, that's the part that triggers the event, then the actual event (also :Connect), then the name of the function defined earlier in the parenthesis.

What other events are there?

You can find out all the events for a specific instance by going to the "View" tab and selecting "Object Browser."

img|22.5x5

From there, select an instance from the list, and to the right, everything with a little lightning icon is an event that can be triggered using the instance you selected. As an example, here's all of the events for the "Part" instance, which is just any part.

img|13.75x5

Quick tip: If you're trying to make something happen when you hover over a part with your mouse, for instance it'd go slightly transparent, it wouldn't be in Part events, but instead Mouse events.

Well then, that's just about everything you need to know about events, thank you for reading this tutorial, hopefully I was helpful to you. Happy coding!

View in-game to comment, award, and more!