Does anybody know why my queue system code won't work

Hello, ive been working on a match based game and I needed a queue system I got my scripter to do it and it was working fine until i made an update then it just stopped working after the countdown your not teleported it just sits on 0 and doesn’t tp I checked the place number too

local queue = workspace:WaitForChild("Queue")
local queueContainer = require(game.ReplicatedStorage.ZonePlus).new(queue.JoinCollider)
local teleportService = game:GetService("TeleportService")
local players = game:GetService("Players")

-- Make sure the PlaceId is correct
local teleportPlaceId = 102398531446197 
local countdownTime = 10
local minimumPlayerRequirements = 1
local standardPlayerRequirements = 2

local timer = queue.Screen.Timer
local countdownActive = false
local currentCountdownTask = nil

-- Update queue title based on player count
local function updateQueueTitle()
	local playerCount = #queueContainer:getPlayers()
	queue.Screen.SurfaceGui.Top.Title.Text = "Starting in .. or when full ("..playerCount.."/"..standardPlayerRequirements..")"
end

-- Add player to UI list
local function addTemplate(player)
	local template = game.ReplicatedStorage.UIComponents.QueuePlayer:Clone()
	template.Name = player.Name
	template.Parent = queue.Screen.SurfaceGui.List
	template.PlayerName.Text = player.Name
	template.Image.Image = players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size180x180)
end

-- Remove player from UI list
local function removeTemplate(player)
	local playerTemplate = queue.Screen.SurfaceGui.List:FindFirstChild(player.Name)
	if playerTemplate then
		playerTemplate:Destroy()
	end
end

-- Function to start the countdown
local function startCountdown()
	-- Exit if countdown already active or not enough players
	if countdownActive or #queueContainer:getPlayers() < minimumPlayerRequirements then return end

	-- Start the countdown
	countdownActive = true
	timer.Value = countdownTime

	currentCountdownTask = task.spawn(function()
		while timer.Value >= 0 do
			-- Check if players drop below the minimum required
			if #queueContainer:getPlayers() < minimumPlayerRequirements then
				countdownActive = false
				return 
			end

			-- Update queue title with the remaining countdown
			queue.Screen.SurfaceGui.Top.Title.Text = "Starting in " .. timer.Value .. "s or when full (".. #queueContainer:getPlayers().. "/".. standardPlayerRequirements ..")"
			task.wait(1)
			timer.Value -= 1
		end

		-- After countdown reaches 0, attempt teleport if players are enough
		if countdownActive and #queueContainer:getPlayers() >= minimumPlayerRequirements and timer.Value == 0 then
			print("Countdown finished, teleporting players...")

			for _, player in pairs(queueContainer:getPlayers()) do
				-- Print to debug which players are being teleported
				print("Attempting to teleport player: ", player.Name)

				-- Try to teleport each player
				local success, errorMessage = pcall(function()
					teleportService:Teleport(teleportPlaceId, player)
				end)

				-- Handle teleport success/failure
				if success then
					print("Successfully teleported: ", player.Name)
				else
					warn("Failed to teleport "..player.Name..": "..errorMessage)
				end
			end
		else
			-- Print error if the countdown finished but teleport condition wasn't met
			warn("Teleport attempt failed: Not enough players or countdown was interrupted.")
		end

		-- Reset countdown state after attempting teleport
		countdownActive = false
	end)
end

-- Function to stop the countdown
local function stopCountdown()
	if currentCountdownTask then
		task.cancel(currentCountdownTask)
		currentCountdownTask = nil
		countdownActive = false
	end
end

-- Function to reset the countdown when needed
local function resetCountdown()
	stopCountdown()
	startCountdown() 
end

-- Event when a player enters the queue
queueContainer.playerEntered:Connect(function(player)
	updateQueueTitle()

	-- Add player to queue UI list
	if not queue.Screen.SurfaceGui.List:FindFirstChild(player.Name) then
		addTemplate(player)
	end
	print(player.Name.." has entered the queue!")

	-- Start countdown if minimum players are met
	if #queueContainer:getPlayers() >= minimumPlayerRequirements then
		resetCountdown() 
	end
end)

-- Event when a player exits the queue
queueContainer.playerExited:Connect(function(player)
	updateQueueTitle()
	removeTemplate(player)

	-- Stop countdown if below the minimum player requirements
	if #queueContainer:getPlayers() < minimumPlayerRequirements then
		stopCountdown() 
	else
		resetCountdown() 
	end

	-- If no players are in the queue, reset title
	if #queueContainer:getPlayers() == 0 then
		queue.Screen.SurfaceGui.Top.Title.Text = "Starting in .. or when full ("..#queueContainer:getPlayers().."/"..standardPlayerRequirements..")"
	end

	print(player.Name.." has left the queue!")
end)

Sorry, I think this post belongs in #help-and-feedback:scripting-support

2 Likes

Timer.value will not equal zero because your loop breaks runs while it’s greater than or equal to zero

Change the loop to check if it’s only > 0, or change the other part of the code that’s == 0

1 Like

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