ffrostfall/ByteNet

Consistent error when firing events on PlayerAdded.

Closed this issue · 2 comments

In cases where you are firing events on PlayerAdded it is highly likely that the following error will be produced:
image

This seems to occur because perPlayerReliable[player]/perPlayerReliable[player] are nil.

I was able to fix this issue by making the following modications to the server module:

server.start:

[...]
local function playerAdded(player: Player)
	if not perPlayerReliable[player] then
		perPlayerReliable[player] = create()
	end
	
	if not perPlayerUnreliable[player] then
		perPlayerUnreliable[player] = create()
	end		
end

for _, player in Players:GetPlayers() do
	playerAdded(player)
end
Players.PlayerAdded:Connect(playerAdded)
[...]

server.sendPlayerReliable:

function serverProcess.sendPlayerReliable(
	player: Player,
	id: number,
	writer: (value: any) -> (),
	data: { [string]: any }
)
	if not perPlayerReliable[player] then
		perPlayerReliable[player] = create()
	end
	load(perPlayerReliable[player])

	alloc(1)
	u8(id)
	writer(data)

	perPlayerReliable[player] = bufferWriter.export()
end

server.sendPlayerUnreliable:

function serverProcess.sendPlayerUnreliable(
	player: Player,
	id: number,
	writer: (value: any) -> (),
	data: { [string]: any }
)
	if not perPlayerUnreliable[player] then
		perPlayerUnreliable[player] = create()
	end
	load(perPlayerUnreliable[player])

	alloc(1)
	u8(id)
	writer(data)

	perPlayerUnreliable[player] = bufferWriter.export()
end

Solved in the next version

This issue still occurs as of 0.4.2.