Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local events,modules = {},{}
- local globalConnections = {}
- local initalisedServices = {}
- local HttpService = game:GetService("HttpService")
- local RunService = game:GetService("RunService")
- local module = {}
- module.__index = module
- function module.new(m,e)
- events = e
- modules = m
- local self = {
- ["_connections"] = {}
- }
- setmetatable(self,module)
- table.insert(initalisedServices,self)
- return self
- end
- function module:ListenToEvent(Args)
- local EventName,Callback = table.unpack(Args)
- local EventObject = events[EventName]
- local Assert = modules.Utils.Assert
- Assert(self._connections[EventName],("Warning: this event is already being listened to by this function (%s)"):format(EventName))
- if Assert(not Callback,("Callback required %s"):format(EventName)) then
- return false
- end
- if Assert(not EventObject,("Cannot find event %s"):format(EventName)) then -- checks if the event exists
- Callback(nil) -- send error to the callback
- return false
- end
- local EventCallType = (if EventObject:IsA("RemoteEvent") then 1 else 2)
- local connection = nil
- local key = HttpService:GenerateGUID(false)
- if RunService:IsServer() then
- if EventCallType == 1 then
- connection = EventObject.OnServerEvent:Connect(function(...) -- we use all of these to be sure we get all possible data from the event
- self._connections[EventName..key] = connection
- globalConnections[EventName..key] = connection
- Callback(...)
- end)
- else
- -- theres no connection for remote functions safly
- EventObject.OnServerInvoke = function(...)
- local args = table.pack(...)
- if Callback then
- task.spawn(function()
- local Return_Data = Callback(table.unpack(args)) -- we have to use this instead of ... or it errors
- repeat
- task.wait()
- until Return_Data
- return Return_Data
- end)
- end
- end
- end
- elseif RunService:IsClient() then
- if EventCallType == 1 then
- connection = EventObject.OnClientEvent:Connect(function(...) -- we use all of these to be sure we get all possible data from the event
- self._connections[EventName..key] = connection
- globalConnections[EventName..key] = connection
- Callback(...)
- end)
- else
- -- theres no connection for remote functions safly
- EventObject.OnClientInvoke = function(...)
- local args = table.pack(...)
- if Callback then
- task.spawn(function()
- local Return_Data = Callback(table.unpack(args)) -- we have to use this instead of ... or it errors
- repeat
- task.wait()
- until Return_Data
- return Return_Data
- end)
- end
- end
- end
- end
- end
- function module:Cleanup(global)
- if modules.Utils.Assert(global == true,"Starting global disconnection") then
- for k,connection in pairs(globalConnections) do
- warn(("Disconnecting %s"):format(k))
- connection:Disconnect()
- end
- for _,service in pairs(initalisedServices) do -- clear all the _connections of all initialised services
- warn("Removing service connections")
- service._connections = {}
- end
- else
- warn("Starting local disconnection")
- for k,connection in pairs(self._connections) do
- warn(("Disconnecting %s"):format(k))
- connection:Disconnect()
- globalConnections[k] = nil -- remove this connection from the global connetions
- end
- end
- end
- function module._Cleanup() -- we dont need a global arg for this because if its called it can only be global
- for k,connection in pairs(globalConnections) do
- warn(("Disconnecting %s"):format(k))
- connection:Disconnect()
- end
- for _,service in pairs(initalisedServices) do -- clear all the _connections of all initialised services
- warn("Removing service connections")
- service._connections = {}
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement