🚩 Événements
🎒 Exemples
info
Remote means the other side, e.g.: if I'm the Client, the remote is the Server. If I'm the Server, the remote is the Client.
Basic Usage
Client/Index.lua
-- register for a local Event (local = client only)
Events.Subscribe("MyLocalEvent", function(my_text)
Console.Log("Event received locally! " .. my_text)
-- outputs "Event received locally! hello nanos world!"
end)
-- calls a local Event in all Local Packages
Events.Call("MyLocalEvent", "hello nanos world!")
-- register for a server Event (remote = server)
Events.SubscribeRemote("MyClientEvent", function(my_text)
Console.Log("Event received from server! " .. my_text)
-- outputs "Event received from server! hello nanos world!"
end)
-- calls a remote Event in all Server Packages
Events.CallRemote("MyServerEvent", Reliability.Reliable, "hello nanos world!")
info
On Server, registering for remote events has an additional first parameter: Player, which is the client who sent the event.
Server/Index.lua
-- register for a local Event (local = server only)
Events.Subscribe("MyLocalEvent", function(my_text)
Console.Log("Event received locally! " .. my_text)
-- outputs "Event received locally! hello nanos world!"
end)
-- calls a local Event in all Local Packages
Events.Call("MyLocalEvent", "hello nanos world!")
-- register for a client Event (remote = client)
Events.SubscribeRemote("MyServerEvent", function(player, my_text)
Console.Log(player:GetName() .. " sent an event from client! " .. my_text)
-- outputs "Syed sent an event from client! hello nanos world!"
-- sends an "answer" to the player which sent this event
Events.CallRemote("MyClientEvent", player, Reliability.Reliable, "hello nanos world! message only for you!")
end)
-- sends a remote Event to all Players in all Client Packages
Events.BroadcastRemote("MyClientEvent", Reliability.Reliable, "hello nanos world!")
Passing entities through Events
Server/Index.lua
-- registers for a local Event
Events.Subscribe("MyEvent", function(my_text, my_vector, my_character, my_number)
Console.Log("Event received! " .. my_text .. " " .. my_vector.X .. " " .. my_character:GetViewMode() .. " " .. my_number)
-- outputs "Event received! hello nanos world! 123 1 456"
end)
-- passing Characters through events
local my_temp_character = Character(Vector(0, 0, 100), Rotator(), "nanos-world::SK_Male")
-- calls a local Event in all Server Packages
Events.Call("MyEvent", "hello nanos world!", Vector(123, 123, 123), my_temp_character, 456)
-- calls a remote Event in all Client Packages of all Players
Events.BroadcastRemote("MyEvent", Reliability.Reliable, "hello nanos world!", Vector(123, 123, 123), my_temp_character, 456)
Client/Index.lua
-- registers for the remote Event, the Character arrives as the same entity on the client
Events.SubscribeRemote("MyEvent", function(my_text, my_vector, my_character, my_number)
Console.Log("Remote event received! %s %s", my_text, tostring(my_character))
end)
note
Events.Subscribe only receives local events (Events.Call), and Events.SubscribeRemote only receives remote ones. This is the behavior since the version 1.22 (see Compatibility Versions).
tip
You can find more examples and a comprehensive guide at the Events Guide page.