Skip to content

Conversation

@sodersys
Copy link

@sodersys sodersys commented Nov 2, 2025

.wait() stores index as length of listeners.
if there's multiple waiting listeners, they will cause eachother's index to be offset when removed. This will lead to loss non-temporary listeners.
by reversing execution order the possiblity of this is removed.

an example of this is below.

--shared
local bytenet = require(game.ReplicatedStorage.ByteNet)

return bytenet.defineNamespace("meow", function()
    return {
        meow = bytenet.definePacket({
            value = bytenet.string
        })
    }
end)
--client
local mod = require(game.ReplicatedStorage.ModuleScript)

for i = 1, 2 do
    task.spawn(function()
        local tv = mod.meow.wait()
        print(tv, i)
    end)
end
--server
local m = require(game.ReplicatedStorage.ModuleScript)

m.meow.sendToAll("awa")
--current wait implimentation
	function exported.wait()
		-- define it up here so we can use it to disconnect
		local index: number

		local runningThread = coroutine.running()
		table.insert(listeners,@native function(data, player)
			task.spawn(runningThread, data, player)

			-- Disconnects the listener
			table.remove(listeners, index)
		end)

		-- we connected, time to set the index for when we need to disconnect.
		index = #listeners

		-- the listener will resume the thread
		return coroutine.yield()
	end

…oval.

.wait() stores index as length of listeners.
if there's multiple waiting listeners, they will cause eachother's index to be offset when removed.
This will lead to loss non-temporary listeners.
by reversing execution order the possiblity of this is removed.

an example of this is below.

--shared
local bytenet = require(game.ReplicatedStorage.ByteNet)

return bytenet.defineNamespace("meow", function()
    return {
        meow = bytenet.definePacket({
            value = bytenet.string
        })
    }
end)

--client
local mod = require(game.ReplicatedStorage.ModuleScript)

for i = 1, 2 do
    task.spawn(function()
        local tv = mod.meow.wait()
        print(tv, i)
    end)
end

--server
local m = require(game.ReplicatedStorage.ModuleScript)

m.meow.sendToAll("awa")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant