local F=assert local G=select local h=unpack local l=type local d={}d.__index=d function d.new()local F=setmetatable({_connections={};_destroyed=false;_firing=false;_bindable=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")},d)return F end function d.Fire(F,...)local h=F._connections F._args={...}F._numArgs=G("\035",...)F._bindable:Fire()end function d.Wait(F)F._bindable.Event:Wait()return h(F._args,1,F._numArgs)end function d.Connect(d,G)F(not d._destroyed,"\067\097\110\110\111\116 \099\111\110\110\101\099\116 \116\111 \100\101\115\116\114\111\121\101\100 \101\118\101\110\116")F(l(G)=="\102\117\110\099\116\105\111\110","\065\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \102\117\110\099\116\105\111\110")return d._bindable.Event:Connect(function()G(h(d._args,1,d._numArgs))end)end function d.DisconnectAll(F)F._bindable:Destroy()F._bindable=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")end function d.Destroy(F)if F._destroyed then return end F._destroyed=true F._bindable:Destroy()end return d
-- Event
-- Crazyman32
-- March 17, 2017

--[[
	
	event = Event.new()
	
	event:Fire(...)
	event:Wait()
	event:Connect(functionHandler)
	event:DisconnectAll()
	event:Destroy()
	
	
	Using 'Connect':

		connection = event:Connect(func)
			connection.Connected
			connection:Disconnect()
	

	-----------------------------------------------------------------------------

	NOTE ON MEMORY LEAK PREVENTION:
		If an event is no longer being used, be sure to invoke the 'Destroy' method
		to ensure that all events are properly released. Failure to do so could
		result in memory leaks due to connections still being referenced.

	WHY NOT BINDABLE EVENTS:
		This module passes by reference, whereas BindableEvents pass by value.
		In other words, BindableEvents will create a copy of whatever is passed
		rather than the original value itself. This becomes difficult when dealing
		with tables, where passing by reference is usually most ideal.
	
--]]



local ASSERT  = assert
local SELECT  = select
local UNPACK  = unpack
local TYPE    = type


local Event = {}
Event.__index = Event


function Event.new()
	
	local self = setmetatable({
		_connections = {};
		_destroyed = false;
		_firing = false;
		_bindable = Instance.new("BindableEvent");
	}, Event)
	
	return self
	
end


function Event:Fire(...)
	local connections = self._connections
	self._args = {...}
	self._numArgs = SELECT("#", ...)
	self._bindable:Fire()
end


function Event:Wait()
	self._bindable.Event:Wait()
	return UNPACK(self._args, 1, self._numArgs)
end


function Event:Connect(func)
	ASSERT(not self._destroyed, "Cannot connect to destroyed event")
	ASSERT(TYPE(func) == "function", "Argument must be function")
	return self._bindable.Event:Connect(function()
		func(UNPACK(self._args, 1, self._numArgs))
	end)
end


function Event:DisconnectAll()
	self._bindable:Destroy()
	self._bindable = Instance.new("BindableEvent")
end


function Event:Destroy()
	if (self._destroyed) then return end
	self._destroyed = true
	self._bindable:Destroy()
end


return Event