Packet - Networking library
Features
Batching "Batches all your events into a single event"
Serialize "Serializes all data into one buffer"
Many types "16 bit floats + 24 bit floats + many more"
DDoS protection "Stops exploiters from crashing the server"
Remote Functions "Safely request a response from the client or server"
Unreliable "UnreliableRemoteEvent support"
Easy To Use "Simple and elegant"
Support My Work
If you liked my work and want to donate to me you can do so here
SourceCode
You can get the sourcecode to this module herePermission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
Documentation
CONSTRUCTOR
Constructor(name: string, parameters: ...)
"Returns previously created packet else a new packet"
PROPERTIES
ResponseTimeout number 10
"Seconds until a response will timeout"
ResponseTimeoutValue any nil
"Value passed back if a response times out"
OnServerInvoke function nil
"Function called when packet is invoked from the client"
OnClientInvoke function nil
"Function called when packet is invoked from the server"
EVENTS
OnServerEvent(player: Player, parameters: ...) Signal
"Fires when the client fires a packet"
OnClientEvent(parameters: ...) Signal
"Fires when the server fires a packet"
METHODS
Response(parameters: ...) Packet
"Converts a RemoteEvent packet to a RemoteFunction packet"
Fire(parameters: ...) any
"Fires a packet to all clients or to the server, if Response() was called will also return a response"
FireClient(player: Player, parameters: ...) any
"Fires a packet to a client, if Response() was called will also return a response"
Serialize(parameters: ...) buffer {Instance}?
"Serializes parameters into a buffer and maybe a array of instances"
Deserialize(buffer {Instance}?) ...any
"Deserializes a buffer and maybe instances into parameters"
Destroy()
"Destroys the packet [you should never need to do this]"
Examples
Simple Example
-- Server
local Packet = require(game.ReplicatedStorage.Packet)
local packet = Packet("PacketName", Packet.NumberS8, Packet.Vector3F32, Packet.Instance)
local player = game.Players.PlayerAdded:Wait()
packet:FireClient(player, -12, Vector3.zero, workspace.SpawnLocation)
-- Client
local Packet = require(game.ReplicatedStorage.Packet)
local packet = Packet("PacketName", Packet.NumberS8, Packet.Vector3F32, Packet.Instance)
packet.OnClientEvent:Connect(function(...)
print(...)
end)
Optional Example
local packet1 = Packet("OptionalString", Packet.Any :: string?)
local packet2 = Packet("OptionalNumber", Packet.Any :: number?)
Array Example
local packet = Packet("Array", {Packet.NumberU8})
packet:Fire({1, 6, 11, 3})
Dictionary Example
local packet = Packet("Dictionary", {Key = Packet.NumberF32, AnotherKey = Packet.String})
packet:Fire({Key = 22, AnotherKey = "Hello"})
Response Example
-- Server
local Packet = require(game.ReplicatedStorage.Packet)
local packet = Packet("PacketName", Packet.NumberF16, Packet.String):Response(Packet.Boolean8)
packet.OnServerInvoke = function(player, number, string)
print("OnServerInvoke", player, number, string) -- 2.2, "Text"
return true
end
-- Client
local Packet = require(game.ReplicatedStorage.Packet)
local packet = Packet("PacketName", Packet.NumberF16, Packet.String):Response(Packet.Boolean8)
local response = packet:Fire(2.2, "Text")
print("Server Response:", response) -- true
UnreliableRemoteEvent Wrapper
--!strict
local RunService = game:GetService("RunService")
local Packet = require(game.ReplicatedStorage.Packet)
local unreliablePackets = {
Packet1 = Packet("Packet1", Packet.NumberU8),
Packet2 = Packet("Packet2", Packet.String),
}
for index, packet in unreliablePackets do
local packet = packet :: Packet.Packet
if RunService:IsServer() then
local unreliableRemoteEvent = Instance.new("UnreliableRemoteEvent")
unreliableRemoteEvent.Name = packet.Name
unreliableRemoteEvent.Parent = game.ReplicatedStorage
unreliableRemoteEvent.OnServerEvent:Connect(function(player, ...)
packet.OnServerEvent:Fire(player, packet:Deserialize(...))
end)
packet.Fire = function(packet, ...)
unreliableRemoteEvent:FireAllClients(packet:Serialize(...))
end
packet.FireClient = function(packet, player, ...)
unreliableRemoteEvent:FireClient(player, packet:Serialize(...))
end
else
local unreliableRemoteEvent = game.ReplicatedStorage:WaitForChild(packet.Name)
unreliableRemoteEvent.OnClientEvent:Connect(function(...)
packet.OnClientEvent:Fire(packet:Deserialize(...))
end)
packet.Fire = function(packet, ...)
unreliableRemoteEvent:FireServer(packet:Serialize(...))
end
end
end
return unreliablePackets
Updates
1.1
Fixed vulnerability that allows client to resume a thread if that thread is yielded after a fire()
1.2
No longer possible for the client to make the server print error messages in live games