Event usage||doc136908

September 20, 2022

How to use global events

The global event uses the Event as the main body, calls the GetEvent function to get the event, and then binds the callback function for the event to use. See available global events Event

Register the global event of player login in the main script of the server. When a player logs in, the bound callback function will be executed:

-- Get player login event
local playerLoginEvent = Event:GetEvent("OnPlayerLogin")

--Bind a callback function for the event
playerLoginEvent:Bind(function(entity)
     print("************************")
     print("OnPlayerLogin!!!", entity)
     print("************************")
end)

When you need a custom global event, you need to register the event before you can get the event.

Register the "TestEvent" global custom event in the server-side main script, and then emit the event 3 times in another server-side script, and the corresponding callback function will also be executed 3 times.

--main script

--register "TestEvent" event
local testEvent = Event:RegisterCustomEvent("TestEvent")

--Bind a callback function for the event
testEvent:Bind(function()
     print("************************")
     print("testEvent!!!")
     print("************************")
end)
-- another server script

--Get "TestEvent" event
local testEvent = Event:GetEvent("TestEvent")

-- fire the event
testEvent:Emit()
testEvent:Emit()
testEvent:Emit()

How to use object events

Object events and global events are very similar in use. The biggest difference is that object events cannot register custom events, but only the events defined by the engine can be used, and then the object to be obtained is the object itself. For example, the format of the event to obtain a part object is Part object: GetEvent(eventName), available object events are visible in the corresponding class documentation page.

Register the join instance event of the map object in the server-side main script, and execute the corresponding callback function whenever an object enters the map.

--Get a static map named map001, the default map
local map = World:GetStaticMap("map001")

-- get instance entry event
local instanceEnterEvent = map:GetEvent("OnInstanceEnter")

--Bind a callback function for the event
instanceEnterEvent:Bind(function(inMap, instance)
     print("************************")
     print("OnInstanceEnter!!!", inMap, instance)
     print("************************")
end)

--Generate parts into this map
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)

Unbind method of event callback function

Use the example of adding an instance to the map above, save the binding handle, cancel the binding after generating two parts, and add the instance to the map after canceling, and the callback function will not be executed again.

--Get a static map named map001, the default map
local map = World:GetStaticMap("map001")

-- get instance entry event
local instanceEnterEvent = map:GetEvent("OnInstanceEnter")

--Bind the callback function for the event, save the binding handle
local bindHandle = instanceEnterEvent:Bind(function(inMap, instance)
     print("************************")
     print("OnInstanceEnter!!!", inMap, instance)
     print("************************")
end)

--Generate parts into this map
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)

--destroy handle to unbind
bindHandle:Destroy()

--Generate parts into this map
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)
Instance.new("Part", map.Root)