SlashCo Wiki

SlashCo.WriteOptional

  SlashCo.WriteOptional( any value, function writeFunc, any optionalArgs )

Description

It's useless to use this function on a boolean / when using net.WriteBool as this will only add overhead.
Unless you want to preserve nil values.

Helper networking function that only writes a value if it isn't nil
The receiver code must use SlashCo.ReadOptional

Arguments

1any value
2function writeFunc
3any optionalArgs

Any amount of additional arguments

Example

How this function can be used

if CLIENT then net.Receive("Example", function() local value1 = SlashCo.ReadOptional(net.ReadString) local value2 = SlashCo.ReadOptional(net.ReadString) print("Value1 is " .. tostring(value1)) print("Value2 is " .. tostring(value2)) end) else local exampleTable = { -- No value1 value2 = "Hello World" } util.AddNetworkString("Example") net.Start("Example") SlashCo.WriteOptional(exampleTable.value1, net.WriteString) SlashCo.WriteOptional(exampleTable.value2, net.WriteString) net.Broadcast() end
Value1 is nil Value2 is Hello World