Issues with making a UI based "teleport-to-private-server-system."

Hey, I’ve been at this for a few days now, and just can’t seem to figure it out. I’m trying to create a “Quick Play” feature, I will also make an accompanying Party system, though I’ll tackle that problem when I get to it.

While I’ve made the Quick Play… functional at best, it does have one main bug that I for the life of me just can’t figure out how to fix. Players can join and leave the table by using the buttons, and teleporting them does work. But if someone spams the join button then it seems to just, break. And when there’s only one person in the server, the countdown starts multiple times.

I’m not sure what to do about it, I guess I could make a debounce for the buttons, but that wouldn’t really solve the problem at hand. Am I approaching this in the wrong way? What do you think I should do?

-- Script in ServerScriptService

local RepStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")

local QuickJoin = RepStorage.QuickJoin
local QuickLeave = RepStorage.QuickLeave
local UpdateCount = RepStorage.UpdateCount


local TimerValue = 15
local PlaceID = 5689319470
local PlayerList = {}
local CountDownActive = false

QuickLeave.OnServerEvent:Connect(function(Player)
	if #PlayerList <= 1 then
		CountDownActive = true
		print(Player.Name.." activated QuickLeave. (Only one person)")
		for i = 1, #PlayerList do
			local User = game.Players:FindFirstChild(PlayerList[i].Name)
			if User then
				table.remove(PlayerList, i)
				print("Removed"..Player.Name.." from #PlayerList.")
				UpdateCount:FireAllClients(TimerValue)
				CountDownActive = false
			end
		end
	else
		print(Player.Name.." activated QuickLeave.")
		for i = 1, #PlayerList do
			local User = game.Players:FindFirstChild(PlayerList[i].Name)
			if User then
				table.remove(PlayerList, i)
				print("Removed"..Player.Name.." from #PlayerList.")
			end
		end
	end
end)

local function TeleportPlayers()
	local Teleporting = false
	print("Teleport if more than 0!")
	if #PlayerList > 0 then
		print("Teleporting Player(s)")
		for i = 1, #PlayerList do
			local Player = game.Players:FindFirstChild(PlayerList[i].Name)
			if Player then
			else
				table.remove(PlayerList, i)
			end
		end
		
		local AccessCode = TeleportService:ReserveServer(PlaceID)
		Teleporting = true
		
		TeleportService:TeleportToPrivateServer(PlaceID, AccessCode, PlayerList)
		repeat wait() until #PlayerList <= 0
		Teleporting = false
	end
end

local function CountDown()
	CountDownActive = true
	
	local Timer = TimerValue
	UpdateCount:FireAllClients(Timer)
	for i = 1, Timer do
		Timer = Timer -1
		UpdateCount:FireAllClients(Timer)
		if #PlayerList == 0 then
			print("CountDown Stopped.")
			CountDownActive = false
			UpdateCount:FireAllClients(TimerValue)
			return
		end
		wait(1)
		if #PlayerList == 0 then
			print("CountDown Stopped.")
			CountDownActive = false
			UpdateCount:FireAllClients(TimerValue)
			return
		end
		if Timer == 0 then
			print("Request Teleport")
			TeleportPlayers()
		end
	end
end

QuickJoin.OnServerEvent:Connect(function(Player)
	
	if CountDownActive == false then
		print(Player.Name.." activated QuickJoin.")
		
		local AlreadyInQueue = false
		for i = 1, #PlayerList do
			if i == Player.Name then
				AlreadyInQueue = true
				print("Player is already in Queue.")
			end
		end
		
		if AlreadyInQueue == false then
			if #PlayerList < 12 then
				table.insert(PlayerList, Player)
				print("Added "..Player.Name.." to #PlayerList.")
			end
		end
		UpdateCount:FireAllClients(TimerValue)
		wait(1)
		CountDown()
	else
		if #PlayerList > 0 then
			print(Player.Name.." activated QuickJoin. (V2)")
			
			
			local AlreadyInQueue = false
			for i = 1, #PlayerList do
				if i == Player.Name then
					AlreadyInQueue = true
					print("Player is already in Queue.")
				end
			end
			
			if AlreadyInQueue == false then
				if #PlayerList < 12 then
					table.insert(PlayerList, Player)
					print("Added "..Player.Name.." to #PlayerList.")
				end
			end
		end
	end
end)

If you have problems visualizing the problem or want to see more behind it. I’ve linked the project down below. Thank you for your help!

Lobby Test.rbxl (28.5 KB)

Add a denounce to the buttons, like adding

if cooldown then return end
cooldown = true
wait(2)
cooldown = false -- get rid of this to make the buttons unusable after one click