Because I’m sick of doing
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, e)
if e == "ThisFunction" then
-- do stuff
I created a system that should be easy to connect remote events, to run the code you place a module into a folder called “Events” or can manually hook up a function via a module script I will post below. The connections also have local character respawned, player added and character added.
local runService = game:GetService("RunService")
local x = {}
if runService:IsServer() then
x.serverEvent = {}
x.playerAdded = {}
x.charAdded = {}
else
x.clientEvent = {
["DestructParts"] = require(game.ReplicatedStorage.Client.DestructionEffects.PartDestruction).Effect;
["EquipTool"] = require(game.ReplicatedStorage.Client.Controllers.ToolController).Init
}
x.clientCharConnections = {}
end
return x
“ClientCharConnections” is when the LocalPlayers character is added, its useful for tools to and some other stuff
The code to connect this events is:
local runService = game:GetService("RunService")
local connections = require(script.Parent.Connections)
local remote = runService:IsClient() and connections.clientEvent or connections.serverEvent
local eventMeta = {
__index = function(self, key)
for i,v in pairs(runService:IsClient() and game.ReplicatedStorage.Client.Events:GetDescendants() or game.ServerScriptService.Events:GetDescendants()) do
if v.Name == key and v:IsA("ModuleScript") then
return require(v)
end
end
end
}
local e = setmetatable(remote, eventMeta)
local function ex(tab, ...)
for i,v in pairs(tab) do
v(...)
end
end
local function AddToEvents(tab, location, name)
for i,v in pairs(location:GetDescendants()) do
local x = require(v)
if type(x) == "table" and x[name] then
tab[v.Name] = x[name]
end
end
end
if runService:IsClient() then
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(event, ...)
e[event](...)
end)
AddToEvents(connections.clientCharConnections, game.ReplicatedStorage.Client.Controllers, "Spawned")
else
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, event, ...)
e[event](player, ...)
end)
AddToEvents(connections.charAdded, game.ReplicatedStorage.Shared.Controllers, "CharacterAdded")
AddToEvents(connections.playerAdded, game.ReplicatedStorage.Shared.Controllers, "PlayerAdded")
AddToEvents(connections.charAdded, game.ServerScriptService.Controllers, "CharacterAdded")
AddToEvents(connections.playerAdded, game.ServerScriptService.Controllers, "PlayerAdded")
end
local function connectPlayer(player)
ex(connections.playerAdded, player)
player.CharacterAdded:Connect(function(character)
ex(connections.charAdded, character, player)
end)
end
local function clientConnect(player)
if player == game.Players.LocalPlayer then
player.CharacterAdded:Connect(function(character)
ex(connections.clientCharConnections, character)
end)
end
end
for i,v in pairs(game.Players:GetPlayers()) do
if runService:IsClient() then
clientConnect(v)
else
connectPlayer(v)
end
end
game.Players.PlayerAdded:Connect(runService:IsClient() and clientConnect or connectPlayer)
Let me know what you think I’m curious if this is a effective way or if major issues will result from it.