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