Skip to content

Connect Sync

There is little to no downside of using light.connect(), this function exists for optimizing frequently sent messages in potentially large servers (or for optimizing memory usage.) If that's not you, you should skip this page.

light.connect_sync() does the same thing as light.connect() with one major difference. Thread reuse will not be used with light.connect_sync(), and the function will be called directly. This means if you yield in your callback, you could get a buffer access out of bounds or cause other unexpected internal issues. If you don't know what that implies, use light.connect().

This function can error if there is already a callback connected.

Consider calling light.disconnect() first if this is an issue.

function light.connect_sync

Client Synchronous Errors
1
2
3
4
function connect_sync<Data>(
   message: Message<Data>,
   callback: (Data) -> ()
): ()

function light.connect_sync

Server Synchronous Errors
1
2
3
4
function connect_sync<Data>(
   message: Message<Data>,
   callback: (Player, Data) -> ()
): ()

An example client-side event polling utility using light.connect_sync():

polling.luau
local function poll<Data>(message: Message<Data>)
   local data = {}
   local next_insert = 1

   light.connect_sync(message, function(data)
      data[next_insert] = data
      next_insert += 1
   end)

   local next_index = 1
   local function iter_next(): T?
      if next_index >= next_insert then

         next_index = 1
         next_insert = 1
         data = {}

         return nil
      end

      return data[next_index]
   end

   return iter_next
end

And an example on how to use the above poll util:

attack_handler.client.luau
1
2
3
4
5
6
7
local attacks = poll(messages.attack)

RunService.PreRender:Connect(function()
   for attack_data in attacks do
      print(`{attack_data.plr} attacked at {attack_data.pos}!`)
   end
end)