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:
-- Creating the remote local remote = Instance.new("RemoteEvent", game.ReplicatedStorage) -- Giving the remote name remote.Name = "TutorialRemote" -- Setting The Remote Event Function remote.OnServerEvent:Connect(function(player, text) print(player, text) end)
Client
local text = -- Set this to what you want to be printed -- Getting the remote local remote = game.ReplicatedStorage:WaitForChild("TutorialRemote") -- Calling the remote remote:FireServer(text)
Example of server to client RemoteEvent
Server
local player = -- Set this to the player you want to run the code local text = -- Set this to what you want to be printed -- Creating the remote local remote = Instance.new("RemoteEvent", game.ReplicatedStorage) -- Giving the remote name remote.Name = "TutorialRemote" -- Calling the remote remote:FireClient(player, text)
Client
-- Getting the remote local remote = game.ReplicatedStorage:WaitForChild("TutorialRemote") -- Setting The Remote Event Function remote.OnClientEvent:Connect(function(text) print(text) 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".
local Part = workspace.Part --Any BasePart works. local OnTouch = function(Hit) --Hit is automatically provided by the touch event. print(Hit.Name.." touched me!") end 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,
local Part = game.workspace.Part script.Parent.Touched:connect(function() 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