The code I will have below is a module library called SLib. It’s supposed to be a small library that helps with small things. Overall I think the Events section needs more optimization.
SLib - Roblox - Whole module
local rem = {}
local rs = game:GetService("RunService")
local mainfold
if rs:IsServer()then
if not game.ReplicatedStorage:FindFirstChild("SLibFolder") then
local fold = Instance.new("Folder",game.ReplicatedStorage)
fold.Name = "SLibFolder"
mainfold = fold
else
mainfold = game.ReplicatedStorage.SLibFolder
end
else
mainfold = game.ReplicatedStorage:WaitForChild("SLibFolder")
end
function rem.remote(name)
local event
local type
local connectmethod
if rs:IsServer()then
local par
if not mainfold:FindFirstChild("SLibRemoteEvents") then
local fold = Instance.new("Folder",mainfold)
fold.Name = "SLibRemoteEvents"
par = fold
else
par = mainfold:FindFirstChild("SLibRemoteEvents")
end
event = Instance.new("RemoteEvent",par)
event.Name = name
connectmethod = "OnServerEvent"
type = "serv"
elseif rs:IsClient() then
if not mainfold:FindFirstChild("SLibRemoteEvents") then
warn("Unable to find SLibRemoteEvents")
else
event = mainfold.SLibRemoteEvents:WaitForChild(name)
end
connectmethod = "OnClientEvent"
type = "client"
end
local remote = {
_connections = {};
Event = event;
Connect = function(self,func)
table.insert(self._connections,event[connectmethod]:Connect(func))
end,
Fire = function(self,...)
if type == "client"then
event:FireServer(...)
elseif type == "serv"then
event:FireAllClients(...)
end
end;
FireTo = function(self,...)
if type == "serv"then
local args = {...}
local otherargs = {}
for i=2,#args do
table.insert(otherargs,args[i])
end
event:FireClient(args[1],unpack(otherargs))
end
end,
Destroy = function(self)
for _,v in ipairs(self._connections)do
v:Disconnect()
end
event:Destroy()
end,
}
return remote
end
function rem.func(name)
local event
local type
local connectmethod
if rs:IsServer()then
local par
if not mainfold:FindFirstChild("SLibRemoteFunctions") then
local fold = Instance.new("Folder",mainfold)
fold.Name = "SLibRemoteFunctions"
par = fold
else
par = mainfold:FindFirstChild("SLibRemoteFunctions")
end
event = Instance.new("RemoteFunction",par)
event.Name = name
connectmethod = "OnServerInvoke"
type = "serv"
elseif rs:IsClient() then
if not mainfold:FindFirstChild("SLibRemoteFunctions") then
warn("Unable to find SLibRemoteFunctions")
else
event = mainfold.SLibRemoteFunctions:WaitForChild(name)
end
connectmethod = "OnClientInvoke"
type = "client"
end
local remote = {
_connections = {};
Event = event;
Connect = function(self,func)
table.insert(self._connections,func)
end,
Fire = function(self,...)
if type == "client"then
return event:InvokeServer(...)
elseif type == "serv"then
return event:InvokeClient(...)
end
end;
Destroy = function()
event:Destroy()
end,
}
event[connectmethod] = function()
for _,v in ipairs(remote._connections)do
return coroutine.wrap(v)()
end
end -- how 2 disconnect?
return remote
end
return rem
^ The module I mostly want optimized
I just want to know if there is any way I could optimize this module. Thanks.
PS: I have been thinking of making my code more tidy and cleaning it up a bit but I just don’t know where to start.
How the code above works is that it’s sort of a shortcut for events. Example:
local cash = slib.Events.remote("AddCash")
cash:Connect(function(player,amount)
print(player.Name.." added "..amount.." cash!")
end)
Client:
local cash = slib.Events.remote("AddCash") -- gets cash remote
cash:Fire(10000)
This can be pretty useful as it saves up fuss when making remote events like this