### Send a Packet to All Clients Source: https://ffrostfall.github.io/ByteNet This example demonstrates how to send a defined packet to all connected clients using ByteNet. It shows how to populate a map-based packet with Vector3 keys and string values. ```luau local packets = require(path.to.packets) packets.myPacket.sendToAll({ [Vector3.new(1, 1, 1)] = "10x bandwidth savings compared to RemoteEvents!", [Vector3.new(1, 2, 1)] = "It's significantly more efficient.", [Vector3.new(2, 1, 1)] = "Depending on your data usage,", [Vector3.new(1, 1, 2)] = "it could be 100x less!", }) ``` -------------------------------- ### Client: Send Packet to Server Source: https://ffrostfall.github.io/ByteNet/api/functions/definePacket Example of a client sending a packet with a 'message' field to the server. Ensure the data structure matches the packet definition. ```luau -- Sending to server packets.myPacket.send({ message = "Hello, world!" }) ``` -------------------------------- ### Define Namespace and Packet Source: https://ffrostfall.github.io/ByteNet/api/functions/definePacket Defines a namespace 'messaging' and a packet 'printSomething' within it. The packet expects a string message. This is the starting point for structuring your data. ```luau local ByteNet = require(path.to.bytenet) return ByteNet.defineNamespace("messaging", function() return { printSomething = ByteNet.definePacket({ -- This value field is very important! value = ByteNet.struct({ message = ByteNet.string, }) }) } end) ``` -------------------------------- ### Client: Listen for Incoming Packets Source: https://ffrostfall.github.io/ByteNet/api/functions/definePacket Sets up a listener on the client to receive packets from the server. The callback function receives only the packet data, as the sender is implicitly the server. ```luau packets.myPacket.listen(function(data) print(`server says { data.message }`) end) ``` -------------------------------- ### Server: Listen for Incoming Packets Source: https://ffrostfall.github.io/ByteNet/api/functions/definePacket Sets up a listener on the server to receive packets. The callback function receives the packet data and the player who sent it. It prints the player's UserId and their message. ```luau packets.myPacket.listen(function(data, player) print(`{player.UserId} says { data.message }`) end) ``` -------------------------------- ### Server: Send Packets to Players Source: https://ffrostfall.github.io/ByteNet/api/functions/definePacket Demonstrates various methods for a server to send packets to clients: to all players, to a specific player, or to all except one. The second parameter is always the player object when specified. ```luau -- Sending to all players packets.myPacket.sendToAll({ message = "Hello, players!" }) -- Sending to an individual player local someArbitraryPlayer = Players.You packets.myPacket.sendTo({ message = "Hello, random player!" }, someArbitraryPlayer) -- Sending to all except a certain player local someArbitraryPlayer = Players.You packets.myPacket.sendToAllExcept({ message = "Hello, everyone except one person!" }, someArbitraryPlayer) ``` -------------------------------- ### Define a ByteNet Namespace and Packet Source: https://ffrostfall.github.io/ByteNet This snippet shows how to define a custom namespace and a packet structure using ByteNet's data types. It's useful for organizing your network data and defining the schema for your packets. ```luau local ByteNet = require(path.to.ByteNet) return ByteNet.defineNamespace("example", function() return { vectorStringMap = ByteNet.definePacket({ value = ByteNet.dataTypes.map(ByteNet.dataTypes.vec3(), ByteNet.dataTypes.string()) }) } end) ``` -------------------------------- ### Define Structs for Structured Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Use ByteNet.struct to define structured data with optimized fields. This is ideal for organizing data and sending complex information efficiently. ```lua return ByteNet.defineNamespace("chunks", function() local chunkData = ByteNet.struct({ biome = ByteNet.uint8, seed = ByteNet.int32, }) return { sendChunks = ByteNet.definePacket({ value = ByteNet.array(chunkData) }) } end) ``` -------------------------------- ### Define Maps for Key-Value Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Use ByteNet.map to send dictionary-like data. Both the key and value types must be consistent throughout the map. Maps also have a maximum of 65,536 elements. ```lua return ByteNet.defineNamespace("example", function() return { people = ByteNet.definePacket({ -- [name] = age value = ByteNet.map(ByteNet.string, ByteNet.uint8) }) } end) ``` -------------------------------- ### Define Arrays for Lists of Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Use ByteNet.array to send lists of data. Note that mixed tables have undefined behavior and arrays have a maximum length of 65,536. ```lua return ByteNet.defineNamespace("example", function() return { myCoolPacket = ByteNet.definePacket({ value = ByteNet.array(ByteNet.bool), }) } } ``` -------------------------------- ### Define Optional Types for Conditional Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Use ByteNet.optional to create fields that may or may not be present. This is useful for optional parameters or indicating error states. ```lua return ByteNet.defineNamespace("chunks", function() return { myCoolPacket = ByteNet.definePacket({ value = ByteNet.struct({ -- An "optional" type takes in a parameter. -- This can be anything! You can even have optional arrays. helloWorldString = ByteNet.optional(ByteNet.string) -- This works! eachCharacter = ByteNet.optional(ByteNet.array(ByteNet.string)) }), }) } end) ``` -------------------------------- ### Send Map Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Send a map where keys are strings (names) and values are unsigned 8-bit integers (ages). ```lua local packets = require(path.to.packets) packets.myCoolPacket.sendToAll({ john = 21, jane = 24, dave = 26, you = 162, }) ``` -------------------------------- ### Send Optional Data in a Packet Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials When sending a packet with optional fields, you can omit the field if it's not needed. The system will handle its absence gracefully. ```lua local packets = require(path.to.packets) local randomlyStringOrNil = if math.random(1, 2) == 1 then "Hello, world!" else nil packets.myCoolPacket.sendToAll({ helloWorldString = randomlyAppearingString, -- Note that even if we don't put the "eachCharacter" field here, -- it won't error. This is because it's optional! }) ``` -------------------------------- ### Send Array Data Source: https://ffrostfall.github.io/ByteNet/api/dataTypes/Specials Send a simple array of boolean values. Avoid sending mixed arrays or arrays with holes as they can lead to undefined behavior. ```lua local packets = require(path.to.packets) packets.myCoolPacket.sendToAll({ -- Important to note that mixed arrays/arrays with holes -- shouldn't be sent through. true, false, true }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.