function Signal.Fire(D,...)for D,K in pairs(D.connections)do K.Handler(...)end if D.totalWaiting>0 then local K=table.pack(...)for n,b in pairs(D.waiting)do D.waiting[n]=K end end end Signal.fire=Signal.Fire function Signal.Connect(K,D)if not(type(D)=="\102\117\110\099\116\105\111\110")then error(("\099\111\110\110\101\099\116\040\037\115\041"):format(typeof(D)),2)end local n=K local b=HttpService:GenerateGUID(false)local Z={}Z.Connected=true Z.ConnectionId=b Z.Handler=D K.connections[b]=Z function Z.Disconnect(D)n.connections[b]=nil Z.Connected=false n.totalConnections-=1 if n.connectionsChanged then n.connectionsChanged:Fire(-1)end end Z.Destroy=Z.Disconnect Z.destroy=Z.Disconnect Z.disconnect=Z.Disconnect K.totalConnections+=1 if K.connectionsChanged then K.connectionsChanged:Fire(1)end return Z end Signal.connect=Signal.Connect function Signal.Wait(D)local K=HttpService:GenerateGUID(false)D.waiting[K]=true D.totalWaiting+=1 repeat heartbeat:Wait()until D.waiting[K]~=true D.totalWaiting-=1 local n=D.waiting[K]D.waiting[K]=nil return unpack(n)end Signal.wait=Signal.Wait function Signal.Destroy(D)if D.bindableEvent then D.bindableEvent:Destroy()D.bindableEvent=nil end if D.connectionsChanged then D.connectionsChanged:Fire(-D.totalConnections)D.connectionsChanged:Destroy()D.connectionsChanged=nil end D.totalConnections=0 for K,n in pairs(D.connections)do D.connections[K]=nil end end Signal.destroy=Signal.Destroy Signal.Disconnect=Signal.Destroy Signal.disconnect=Signal.Destroy return Signal

function Signal:Fire(...)
	for _, connection in pairs(self.connections) do
		connection.Handler(...)
	end
	if self.totalWaiting > 0 then
		local packedArgs = table.pack(...)
		for waitingId, _ in pairs(self.waiting) do
			self.waiting[waitingId] = packedArgs
		end
	end
end
Signal.fire = Signal.Fire

function Signal:Connect(handler)
	if not (type(handler) == "function") then
		error(("connect(%s)"):format(typeof(handler)), 2)
	end
	
	local signal = self
	local connectionId = HttpService:GenerateGUID(false)
	local connection = {}
	connection.Connected = true
	connection.ConnectionId = connectionId
	connection.Handler = handler
	self.connections[connectionId] = connection

	function connection:Disconnect()
		signal.connections[connectionId] = nil
		connection.Connected = false
		signal.totalConnections -= 1
		if signal.connectionsChanged then
			signal.connectionsChanged:Fire(-1)
		end
	end
	connection.Destroy = connection.Disconnect
	connection.destroy = connection.Disconnect
	connection.disconnect = connection.Disconnect
	self.totalConnections += 1
	if self.connectionsChanged then
		self.connectionsChanged:Fire(1)
	end

	return connection
end
Signal.connect = Signal.Connect

function Signal:Wait()
	local waitingId = HttpService:GenerateGUID(false)
	self.waiting[waitingId] = true
	self.totalWaiting += 1
	repeat heartbeat:Wait() until self.waiting[waitingId] ~= true
	self.totalWaiting -= 1
	local args = self.waiting[waitingId]
	self.waiting[waitingId] = nil
	return unpack(args)
end
Signal.wait = Signal.Wait

function Signal:Destroy()
	if self.bindableEvent then
		self.bindableEvent:Destroy()
		self.bindableEvent = nil
	end
	if self.connectionsChanged then
		self.connectionsChanged:Fire(-self.totalConnections)
		self.connectionsChanged:Destroy()
		self.connectionsChanged = nil
	end
	self.totalConnections = 0
	for connectionId, connection in pairs(self.connections) do
		self.connections[connectionId] = nil
	end
end
Signal.destroy = Signal.Destroy
Signal.Disconnect = Signal.Destroy
Signal.disconnect = Signal.Destroy



return Signal
