Intro
Network.rbxm (10.6 KB)
Hey everyone, we built a decent shared network module for The Wild West, and I wanted to release it as a free resource for you guys.
This is a single module that you slap into ReplicatedStorage, and it just works. It mimics functionality of Remote Events/Functions, without you having to create them manually.
No more having to instance a new remote event or function, then hooking them up to scripts every time you want to use them in your game!
(It still creates them, but behind the scenes)
Small peak at what it looks like in a game:
API/Use
-- Server API
Network:BindFunctions(functions)
Network:BindEvents(events)
Network:FireClient(client,name,params)
Network:FireAllClients(name,params)
Network:FireOtherClients(ignoreclient,name,params)
Network:FireOtherClientsWithinDistance(ignoreclient,name,distance,params)
Network:FireAllClientsWithinDistance(name,distance,position,params)
Network:LogTraffic(duration)
-- Client API
Network:BindEvents(events)
Network:FireServer(name,params)
Network:InvokeServer(name,params)
Server API Examples
Network:BindFunctions(functions)
--[[
Takes a table of function names and binds the function to it
--]]
Network:BindFunctions({
MyFunction1 = function(client,...)
return true
end,
MyFunction2 = function(client,...)
return false
end
})
Network:BindEvents(events)
--[[
Takes a table of event names and binds the event to it
--]]
Network:BindEvents({
MyEvent = function(client,...)
-- Do thing here
end,
MyEvent2 = function(client,...)
-- Do thing here
end
})
Network:FireClient(client,name,params)
--[[
Fires the event on the given client
--]]
Network:FireClient(game.Players.Player1,"MyEvent","Hello")
Network:FireAllClients(name,params)
--[[
Fires the event on all clients
--]]
Network:FireAllClients("MyEvent","Hello")
Network:FireOtherClients(ignoreclient,name,params)
--[[
Fires the event on all clients EXCEPT for ignoreclient.
Useful if you want the client to do an effect instantly, and then have the effect replicate to others
--]]
Network:FireOtherClients(game.Players.Player1,"MyEvent","Hello")
Network:FireOtherClientsWithinDistance(ignoreclient,name,distance,params)
--[[
Similar to FireOtherClients,
but only fires if their distance to ignoreclient <= distance
--]]
Network:FireOtherClientsWithinDistance(game.Players.Player1,"MyEvent",100,"Hello")
Network:FireAllClientsWithinDistance(name,distance,position,params)
--[[
Fires to all clients <= distance away from position
--]]
Network:FireAllClientsWithinDistance("MyEvent",100,Vector3.new(0,0,0),"Hello")
Network:LogTraffic(duration)
--[[
Logs network traffic over duration,
then prints out debug info so you can see how much traffic is being sent
--]]
Network:LogTraffic(10)
Client API Examples
Network:BindEvents(events)
--[[
Takes a table of event names and binds the event to it
--]]
Network:BindEvents({
MyEvent = function(...)
-- Do thing here
end,
MyEvent2 = function(...)
-- Do thing here
end
})
Network:FireServer(name,params)
--[[
Fires the RemoteEvent attached to name, with the given parameters
--]]
Network:FireServer("MyEvent","Hello")
Network:InvokeServer(name,params)
--[[
Invokes the RemoteFunction attached to name, with the given parameters
--]]
local returned = Network:InvokeServer("MyFunction1","Hello")
print("MyFunction1 returned: ", returned)
Why? (Example Included)
Remotes can be pretty tedious to set up, without any wrappers to do the work for you.
Here’s an example of a before/after for creating some simple remotes to interact between server and client
Example: Client will return a value from the server
then it will send a message to the server, which fires a message back to the client
Before:
-- Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local ReturnThing = Remotes.ReturnThing
local DoThing = Remotes.DoThing
local DoThing2 = Remotes.DoThing2
local value = ReturnThing:InvokeServer()
print("Returned value: ",value)
DoThing:FireServer()
DoThing2.OnClientEvent:connect(function(message)
print(message)
end)
-- Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local ReturnThing = Remotes.ReturnThing
local DoThing = Remotes.DoThing
local DoThing2 = Remotes.DoThing2
ReturnThing.OnServerInvoke = function(client,...)
return true
end
DoThing.OnServerEvent:connect(function(client,...)
print("Do things")
DoThing2:FireClient(client,"Hello") -- Makes client print "Hello"
end)
This doesn’t look too hard at first, but it can get tedious when you have a lot of remotes
This is all the work required with this module:
-- Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Network = ReplicatedStorage.Network
Network:BindEvents({
DoThing2 = function(message)
print(message)
end
})
local value = Network:InvokeServer("ReturnThing")
print("Returned value: ",value)
Network:FireServer("DoThing",...)
-- Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Network = require(ReplicatedStorage.Network)
Network:BindFunctions({
ReturnThing = function(client,...)
return true
end
})
Network:BindEvents({
DoThing = function(client,...)
print("Do things")
Network:FireClient(client,"DoThing2","Hello")
end
})
Why not just use one Remote Event/Function?
Good question! @Tomarty goes over it in some more detail here ORE (One Remote Event) - #33 by Tomarty
But the quick answer is: Sending identification strings can be dangerous, depending on how frequently you send data. An identifier string can result in significantly more data being sent over the network for no reason, which can harm your game’s network performance.
This module doesn’t actually send any extra data over the network for event identification.
Instead, it creates or retrieves the remote event or function to use based on the name you pass in.
So multiple remotes still exist(as many as you register), but you don’t have to create them yourself and hook them up individually
Hope you guys like it! As always, feel free to post feedback or problems here