Questions about connections

Im making a sword combat system and i just have a question about the connection for my hitbox.

local connection
connection = Hitbox.Touched:Connect(function(hit)print(hit.Name)end)
  1. Does the script yield until the event runs?
  2. If i disconnect the connection, the print function wont run anymore?
1 Like
  1. No, different methods of RBXScriptSignal exist that affect thread execution in different ways. :Connect(), :ConnectParallel(), and :Once() do not yield the current thread (that is, your script) in any way other than for instantiation of the connection which is effectively instantaneous. On the other hand, :Wait() applies a yield until the respective event fires, subsequently returning whatever arguments a standard connection would. This is often seen with RenderStepped:
--localplayer scope
local runSvc = game:GetService'RunService';

local dt = runSvc.RenderStepped:Wait(); --yields the code until the client's next rendered frame, then returns the time it took (in seconds) for the new frame to begin

print(dt); --this will be a small number value; we can use whatever :Wait() returned
  1. Correct, you can call :Disconnect() on any RBXScriptConnection to stop the passed in function (during the instantiation) from firing whenever the event fires. It’s always good practice to disconnect unnecessary connections since they are known to be fairly memory-intensive in bulk.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.