What are connections, and how do they work?

I know that events in luau can be connected to, but there’s one thing I don’t get about it.

Recently, I’ve asked someone for help on a custom subspace tripmine that adheres to my game. I was trying to wait for the bomb to be touched so then I could get it to explode, but I sent the user as an argument in the explode function, therefore I asked for help, and a user helped me with a variable named “Connection”.

This “connection” would let me connect to the .touched event and get the “hit” parameter that I ended up needing to find out who was hit by the explosion.

I asked the user’s what this connection thing was but they haven’t responded yet.

So can anyone tell me what these connection variables are?

Code down below.

	Connection = bombClone.Touched:Connect(function(Hit)
		local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)
		if Player and tostring(Player) == tostring(user) then
			Connection:Disconnect() -- Assuming bombClone gets destroyed, this isn't necessary as destroying ANY instance also DROPS any event.
			explode(bombClone, user)
		end
	end)

Connect is not a variable… its just a connection to an event, ur function being exploding the character u will be using connect a lot for example

local Part = workspace.Part
Part.Touched:Connect(function(hit)--when the part gets touched we connect it to some lines of codes
print("yay touched")
end)
heres some documentation:
[https://create.roblox.com/docs/scripting/events](https://create.roblox.com/docs/scripting/events)
1 Like

This doesn’t really answer my question though. I understand what connection does for events, but I don’t understand how the person who fixed my code used a variable named Connection.

The :Connect(fn) is a method of roblox events. This method returns an RBXScriptConnection. This can be stored as a variable like when you return values in a regular function. The RBXScriptConnection is usually used to disconnect the event later when it’s not needed anymore through :Disconnect()

In your case the variable Connection is used to disconnect the touched event when the bomb is touched so that it will only trigger once

Docs:
RBXScriptConnection
RBXScriptSignal

3 Likes

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