ByteNet icon indicating copy to clipboard operation
ByteNet copied to clipboard

Sending data when player joins causes an error

Open birds3345 opened this issue 1 year ago • 1 comments

When you send data immediately after a player joins, an error is produced: ReplicatedStorage.ByteNet.process.bufferWriter:148: attempt to index nil with 'size'

The code used:

local ByteNet = require(game.ReplicatedStorage.ByteNet)

local packets = ByteNet.defineNamespace("a", function()
	return {
		Test = ByteNet.definePacket({
			value = ByteNet.bool;
		});
	}
end)

game.Players.PlayerAdded:Connect(function(player: Player)
	packets.Test.sendTo(true, player)
end)

birds3345 avatar Jul 25 '24 04:07 birds3345

You can fix this for now by using task.defer:

Players.PlayerAdded:Connect(function(player: Player)
	task.defer(packets.Test.sendTo, true, player)
end)

A possible fix on the library side would be checking if the channel exists when firing data, however not including it is a microoptimization and it depends on ffrostfall whether it should be added or not

function serverProcess.sendPlayerReliable(
	player: Player,
	id: number,
	writer: (value: any) -> (),
	data: { [string]: any }
)
    if not perPlayerReliable[player] then
        perPlayerReliable[player] = create()
    end
    perPlayerReliable[player] = writePacket(perPlayerReliable[player], id, writer, data)
end
-- and the same for `sendPlayerUnreliable`

Mark-Marks avatar Aug 07 '24 12:08 Mark-Marks