Queue System Breaking [REPOST]

  1. What do you want to achieve? I’m making a story game so I’m trying to make a queue system

  2. What is the issue? When 1 player enters it, it works fine. But when another player tries to enter it, it breaks and starts removing random players

  3. What solutions have you tried so far? I’ve tried to debug it but nothing happens, it doesnt give an error too

This is my modulescript:

local Region3M = {}

function Region3M:ReturnTouchingParts(Object)
	local PartRegion = Region3.new(Object.Position - Object.Size/2, Object.Position + Object.Size/2)

	return workspace:FindPartsInRegion3(PartRegion, Object)
end

return Region3M

and this is my script:

local TeleportService = game:GetService("TeleportService")

local Players = game:GetService("Players")

local MGame = workspace.Game
local Region3Module = require(script.Region3)

local Region = MGame.Region

local Highlight = MGame.Highlight.Highlight
local SurfaceUI = MGame.Highlight.SurfaceUI

local Count = SurfaceUI.Count

local MaxCount = 6
local Queue = {}

local CountdownNumber = 15

local CountdownStartTick;

local GetCountdown = function()
	return CountdownNumber - (math.floor(tick() - CountdownStartTick))
end

while true do
	local Objects = Region3Module:ReturnTouchingParts(Region)
	
	for Index, Player in pairs(Queue) do
		if not Player.Character then
			table.remove(Queue, Index)

			continue
		end

		if not table.find(Objects, Player.Character.PrimaryPart) then
			table.remove(Queue, Index)
		end
	end
	
	for _, Object in pairs(Objects) do
		if Players:GetPlayerFromCharacter(Object.Parent) and Object == Object.Parent.PrimaryPart then
			local Player = Players:GetPlayerFromCharacter(Object.Parent)
			
			if not table.find(Queue, Player) and #Queue < MaxCount then
				table.insert(Queue, Player)
				
				print("Added!")
			end
		end
	end
	
	if #Queue > 0 and not CountdownStartTick then
		CountdownStartTick = tick()
	elseif #Queue == 0 then
		CountdownStartTick = nil
		
		SurfaceUI.Count.Text = #Queue.." / "..MaxCount
	end
	
	if CountdownStartTick and GetCountdown() > 0 then
		SurfaceUI.Count.Text = #Queue.." / "..MaxCount.."\n"..GetCountdown()
	end
	
	if #Queue >= 0 and #Queue < MaxCount then
		Highlight.FillColor = Color3.fromRGB(0, 255, 0)
	elseif #Queue == MaxCount then
		Highlight.FillColor = Color3.fromRGB(255, 0, 0)
	end
	
	if CountdownStartTick and GetCountdown() == 0 then
		SurfaceUI.Count.Text = "Teleporting..."
		
		Highlight.FillColor = Color3.fromRGB(255, 0, 0)
		
		local Server = TeleportService:ReserveServer(12153090763)

		TeleportService:TeleportToPrivateServer(12153090763, Server, Queue)
		
		Queue = {}
		
		task.wait(5)
	end
	
	task.wait()
end

Can someone help me? I’ve tried all the solutions I can but nothing works.

I can provide a rblx copy of my place if you want

Calling table.remove on a table invalidates (or at least modifies) some iterators over that table. Specifically, any iterators pointing to an element at Index or later. I think xD

For example:

local items = {'a', 'b', 'c', 'd', 'e'}
for i, item in ipairs(items) do
    print(i, item)
    if item == 'c' then table.remove(items, i) end
end

This prints a b c e while you’d expect it to print a b c d e, visiting every element. So in your loop over the players, every time you remove a player the next player gets skipped.

This happens because the way to visit the next element is actually to not increment the index, since every element after the removed element gets shifted down so there are no gaps. The index must remain the same when an element gets removed, because the next element is now in the position of the old index!

You work around it by iterating backwards:

local items = {'a', 'b', 'c', 'd', 'e'}
for i = #items, 1, -1 do
    local item = items[i]
    print(i, item)
    if item == 'c' then table.remove(items, i) end
end

This correctly visits every element, even if some are removed (because none of the subsequent items get shifted around due to table.remove).

I don’t know if that’ll fix your issue though, but you should probably fix it anyway.

Alright, I’ll try that! I’ll let you know if it works. Also I respoted this because my old post didnt get enough attention

It didn’t fix it. My error is that the player gets false flagged, for example:

– Player 1 Joins

Queue = {Player1}

– Player 2 Joins

Queue = {Player1, Player2}
Queue = {Player1}

I’d personally recommend using GetPartBoundsInBox over the region3 method, it doesn’t take into account part orientation, and can cause for positions to be off or misrepresented.

1 Like

Thanks! I really thought my region3 module was correct, anyways I’ll try to use it in more projects!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.