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

What are events and how do they work?

Asked by 6 years ago

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?

0
Which kind of event are you talking about? BindableEvents? OsterDog 14 — 6y
0
I assume he's talking about RemoteEvents Griffi0n 315 — 6y
0
both I mean i dont really understand either MusicalDisplay 173 — 6y
0
He is talking about .Touched, .PlayerAdded,etc RBLXNogin 187 — 4y

3 answers

Log in to vote
1
Answered by
Griffi0n 315 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
Pretty sure he's just talking about events in general, but good post! Tomstah 401 — 6y
Ad
Log in to vote
0
Answered by
Tomstah 401 Moderation Voter
6 years ago

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!

Log in to vote
0
Answered by
smehran 15
6 years ago
Edited 6 years ago

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

Answer this question