I don't understand fully what events do and how they work. I've read a few articles on events and I still cannot understand. What are the benefits of using events? Why are they so important?
If you are talking about RemoteEvents here you go! A RemoteEvent is a way for the client to have the server execute code or to have the server to have the client execute code (although not used as much)
RemoteEvents (client to server) are used to get around FE (Filtering Enabled) and are very useful.
Example of client to server RemoteEvent
Server:
1 | -- Creating the remote |
2 | local remote = Instance.new( "RemoteEvent" , game.ReplicatedStorage) |
3 | -- Giving the remote name |
4 | remote.Name = "TutorialRemote" |
5 |
6 | -- Setting The Remote Event Function |
7 | remote.OnServerEvent:Connect( function (player, text) |
8 | print (player, text) |
9 | end ) |
Client
1 | local text = -- Set this to what you want to be printed |
2 | -- Getting the remote |
3 | local remote = game.ReplicatedStorage:WaitForChild( "TutorialRemote" ) |
4 |
5 | -- Calling the remote |
6 | remote:FireServer(text) |
Example of server to client RemoteEvent
Server
1 | local player = -- Set this to the player you want to run the code |
2 | local text = -- Set this to what you want to be printed |
3 | -- Creating the remote |
4 | local remote = Instance.new( "RemoteEvent" , game.ReplicatedStorage) |
5 | -- Giving the remote name |
6 | remote.Name = "TutorialRemote" |
7 |
8 | -- Calling the remote |
9 | remote:FireClient(player, text) |
Client
1 | -- Getting the remote |
2 | local remote = game.ReplicatedStorage:WaitForChild( "TutorialRemote" ) |
3 |
4 | -- Setting The Remote Event Function |
5 | remote.OnClientEvent:Connect( function (text) |
6 | print (text) |
7 | end ) |
Ah! A good ol' fundamental question!
Events are basically the "triggering" or "firing" of some special thing that happens in ROBLOX. One of the most important ones are ".Touched" This will fire every time a BasePart is "touched" by another BasePart. After finding an event you can "bind" it to a function. This makes it so every time a function is triggered or fired, that the function runs. To bind a function to a event we use ":Connect" Connect's first argument is the function you want to bind it to. Here's an example of using ".Touched".
1 | local Part = workspace.Part --Any BasePart works. |
2 |
3 | local OnTouch = function (Hit) --Hit is automatically provided by the touch event. |
4 | print (Hit.Name.. " touched me!" ) |
5 | end |
6 |
7 | Part.Touched:Connect(OnTouch) --And we're done! |
Simply put, we connected a function that prints the name of something hitting a block and "binded" the function to the event so that every time the event happens (Whenever the Part is touched) it'll fire the function!
Events are basically something that happens when something is triggered, If you open your object browser and maybe go on 'part' scroll down you will see symbols with lightning bolts on them those are events, for example if you wanted a part to be destroyed if it is touched then you can do,
1 | local Part = game.workspace.Part |
2 |
3 | script.Parent.Touched:connect( function () |
4 |
5 | Part:destory() |
It is really simple and you can do a lot of other events with Gui's Parts etc..
You can replace the ****Touched**** with other events. :D