Event hanlder amd listenr

i had asked an ai what is eventhandler and event listner and he told me each one is unique . is it ? i i need an example of what is handler and listener please

Event handlers and listeners are a very good and memory-efficient structure to manage code that runs on events. Events will fire whenever specific things happen, for instance the BasePart.Touched event will be fired whenever something touches the part. A callback can be provided to run certain code when this event is fired, for example:

Part.Touched:Connect(function(hit: BasePart)
    print(hit)
end)

Now, whenever that part is touched, the part that touched it will be printed because that function will be run.




Event structure:

  • When something specific happens, the listener will fire the signal.
  • The signal will run any connected callbacks when the signal is fired.
  • The code within the callbacks are run.
  • These callbacks are the handlers.

This structure means that code is only run when it needs to be in relation to an event and you don’t have too much code constantly listening for something to happen.

Hope this helps!

like what @12345koip said, they listen to events and send events, The best way to describe it is with a remote event. In this case were going to make a system that communicates with the server and sends information to use later on.

Handler on client

local Event = game.ReplicatedStorage.RemoteEvent-- Get the remote Event

Event:FireServer("Hello World!")-- Fire it and pass the string parameter

Listener on Server

local Event = game.ReplicatedStorage.RemoteEvent

Event.OnClientEvent:Connect(function(string) -- Listen for the event, and check get the parameters sent
print(string)
end)

This is passing information from the client and the server Listens to the event.

i know what and how events work i just dont know what is event handler it confuses me sometimes and listener too

what about the .Touched is it listener or handler?

The listener is what listens for the event and invokes the connected callbacks. The handlers are the connected functions, just as I said in my post.

1 Like

.Touched is an event. Roblox will have some C++ code which is the listener; it checks for when another part comes in contact with the part. This event is then fired. The handlers, which are connected with :Connect, will then run code you want to happen on the event.

1 Like

can you give me a quick example about handler and listener to fully understand it please im still confused

so @JAcoboiskaka1121 was wrong about his example right ?

I’ll give a custom implementation example using a custom handler, it’d probably be easier to understand that way.

Let’s say you had a system that controls group rewards. The player walks into a zone and a script checks to see if the player is in a group. If they are, it lets another script know about this.

  • Script A detects the part touch. It then checks to see if the player is in the group.
  • Script A holds the listener which fires a signal when a player successfully claims group rewards. It also contains the logic to claim the rewards.
Part.Touched:Connect(function(hit: BasePart)
    --get player, check rewards, etc. etc.
    if isInGroup then --if the player is in the group, fire the signal to alert another script
        --pass the player along with this
        GroupRewardsSignal:Fire(player)
    end
end)
  • Script B will receive this. Let’s say Script B is to add 200 coins to the player’s leaderstats.
  • Script B is the handler which handles logic connected to this event. Code here will run as a result of the event being fired.
--whenever Script A fires the signal, Script B will run this code because it is connected to the signal
GroupRewardsSignal:Connect(function(player: Player)
    --add money, etc. etc.
    print(player.Name, "just claimed GroupRewards!")
end)

No. RemoteEvents are another example of a listener-handler situation. In that case, you can’t actually access the listener.

  • Client wants to send information to the server. They call the :FireServer() method. The RemoteEvent itself contains the listener code which listens for this (in this case, it’s the :FireServer() method itself)
  • The RemoteEvent class will then pass information sent from the client to the handler, which is connected to the .OnServerEvent signal.




The listener is the code that “listens” for an action; it waits for something to happen and then fires the signal.

The handler(s) contain code which is to be run on the signal. The handlers are connected to the signal so that the code is run when the signal is fired.

i ve read this reply like 4 times yet i still dont understand something the examples u ve provided about script a and b saying a is listner and b is handler and saying the example of @JAcoboiskaka1121 isnt wrong i think there is a contradiction here

Imagine it like a letter. Person A wants to alert Person B about something that’s happened. They send a letter to Person B. Person B receives the letter and goes to do something (idk) with the information.

  • Person A is the listener. Something triggered them to send the letter.
  • The letter is the signal. Person A writes information in it and sends it (the same way Script A can pass information to Script B through a signal)
  • Person B is a handler. They’ve received the letter and been told something has happened along with the information Person A put in the letter. They then go out and do something because they received the letter.

by what i ve understood here i think these two are opposite no?

They’re not really the opposite. It’s kind of the same thing as the letter analagy, except there’s not really a set trigger.

  • Client wants to send the server some information. It writes the information in a letter and sends it (RemoteEvent:FireServer())
  • The server receives the letter through the letterbox and does things based on the contained information (a callback connected to RemoteEvent.OnServerEvent)

thank you so mich for staying with and i apperciate it but… it is hard for my rusty brain to understand🥺

It’s fine! Just let me know what you still don’t understand. I’m happy to help.

(I might not reply for a few hours because ima sleep now)

1 Like

so the fire:server/client is the listner? and what is callback?

The callback is what function you connect to the event.

local function callback(player: Player, ...: any) --this is the callback function
    print("This is the callback to the OnServerEvent event.")
end

--connect the callback to the event
RemoteEvent.OnServerEvent:Connect(callback)
1 Like

if im understanding correctly function= callback?

Yeah, as long as it’s connected, it’s a callback. You can have multiple callbacks.

local function callback1()
    print("this is callback number 1")
end

local function callback2()
    print("this is callback number 2")
end

Event:Connect(callback1)
Event:Connect(callback2)

Event:Fire() --> both callbacls are run
1 Like