Examples
Round trip a Schema
const VoidSentryUltimate = require(path.to.VoidSentryUltimate)
const Types = VoidSentryUltimate.Types
const Schema = VoidSentryUltimate.Schema
const Profile = Schema {
Coins = Types.U32,
DisplayName = Types.String8,
EquippedItem = Types.Optional(Types.U16),
}
const Bytes = Profile.Serialize {
Coins = 12_500,
DisplayName = "Elentium",
EquippedItem = 17,
}
const Restored = Profile.Deserialize(Bytes)
print(Restored.DisplayName, Restored.Coins, Restored.EquippedItem)
Encode a compact inventory
When item identifiers fit from 0 through 255, an array of U8 values keeps
both the element representation and count compact:
const Inventory = Types.Array8(Types.U8)
const Bytes = VoidSentryUltimate.Serialize(Inventory, { 4, 9, 23, 71 })
const ItemIds = VoidSentryUltimate.Deserialize(Inventory, bytes)
Array8 adds a one-byte element count, and every U8 item adds one byte. Use a
wider count or item node if either limit can be exceeded.
Send a RemoteEvent payload
Define the schema in a shared module so the server and client cannot accidentally drift.
const VoidSentryUltimate = require(path.to.VoidSentryUltimate)
const Types = VoidSentryUltimate.Types
const Schema = VoidSentryUltimate.Schema
const DamageMessage = Schema {
Amount = Types.U16,
Critical = Types.Bool,
Origin = Types.VectorF24,
Target = Types.Instance,
}
return DamageMessage
Serialize on the server:
const Bytes = DamageMessage.Serialize {
Amount = 25,
Critical = false,
Origin = Vector3.new(10, 2, -4),
Target = TargetPart,
}
DamageEvent:FireClient(Player, Bytes)
Deserialize on the client with the same schema:
DamageEvent.OnClientEvent:Connect(function(Bytes: buffer)
const Message = DamageMessage.Deserialize(Bytes)
print(Message.Target, Message.Amount)
end)
Types.Instance serializes based on the instance ID (reference). Use Types.SerInstance instead only when the receiver should create a new instance from serialized properties.
Serialize an instance by value
const SerializablePart = Types.SerInstance("Part", {
Anchored = Types.Bool,
Color = Types.Color3,
Name = Types.String8,
Position = Types.Vector,
Size = Types.Vector,
})
const Bytes = VoidSentryUltimate.Serialize(SerializablePart, workspace.SourcePart)
const CopiedPart = VoidSentryUltimate.Deserialize(SerializablePart, Bytes)
CopiedPart.Parent = workspace
Deserialization creates a new Part and assigns the listed properties. Parent is not included unless it is explicitly part of the schema.
You can also define children in the schema
const PartWithChild = Types.SerInstance("Part", {
Name = Types.String8,
Child = Types.Instance
})
Pack eight flags
const Flags = {
true,
true,
false,
false,
true,
false,
false,
true,
}
const OneByte = VoidSentryUltimate.Serialize(Types.BoolPacked, Flags)
const RestoredFlags = VoidSentryUltimate.Deserialize(Types.BoolPacked, OneByte)
BoolPacked always represents exactly eight booleans.
Choose F24 or F32 deliberately
For three-dimensional positions, VectorF24 uses 9 bytes while Vector uses
12 bytes:
const CompactPosition = Types.VectorF24
const ExactF32Position = Types.Vector
const Compact = VoidSentryUltimate.Serialize(CompactPosition, Position)
const Regular = VoidSentryUltimate.Serialize(ExactF32Position, Position)
VectorF24 is lossy and has less range and precision. Round-trip values from
your actual game and compare them against your accepted positional tolerance
before choosing it. Use Vector when normal F32 behavior is required.
Quantize UI scales and compact CFrames
UDim2Quant stores only scale, mapped into 16-bit values over a chosen range.
CFrameQuantF16 and CFrameQuant8F16 store F16 position plus quantized Euler
angles:
const Scale = Types.UDim2Quant(0, 1)
const CompactCFrame = Types.CFrameQuantF16
const SmallerCFrame = Types.CFrameQuant8F16
const ScaleBytes = VoidSentryUltimate.Serialize(Scale, UDim2.fromScale(0.5, 0.25))
const CframeBytes = VoidSentryUltimate.Serialize(CompactCFrame, Cframe)
Decoded UDim2Quant offsets are zero. CFrameQuant8F16 is smaller (9 bytes)
than CFrameQuantF16 (12 bytes) and coarser in orientation.
Preserve an application header
const Payload = Types.Struct {
Id = Types.U32,
Message = Types.String8,
}
const Bytes = VoidSentryUltimate.SerializeWithOffset(Payload, {
Id = 7,
Message = "ready",
}, 2)
buffer.writeu8(Bytes, 0, 1)
buffer.writeu8(Bytes, 1, 9)
const Decoded = VoidSentryUltimate.DeserializeWithOffset(Payload, Bytes, 2)
The first two bytes are initially zero because SerializeWithOffset allocates a new zero-filled buffer of offset + payload length.
Inherit schema fields from another schema
const Base = Schema {
Name = Types.String8,
Id = Types.UInt
}
const Hero = VoidSentryUltimate.InheritedSchema({
Health = Types.UInt,
Damage = Types.UInt
}, Base)
const Bytes = Hero.Serialize {
Name = "Hero1",
Id = 5,
Health = 100,
Damage = 10
}
VoidSentryUltimate.InheritedSchema constructs a Schema object from the passed arguments (either a table with [string]: SerdesNode, Struct, or another Schema)
There is also a VoidSentryUltimate.Types.InheritedStruct which accepts the same thing but creates a Struct node