Lobby queue and teleport script is really buggy

Hi everyone! I work as a game dev instructor teaching kids how to make games in Roblox. I work primarily on the art side of game dev, but I know my way around scripting in Roblox. I’m currently helping a student work on their game’s lobby system where much of the scripting was done by another instructor. I was able to clean up some of the script to get it to at least kind of work, but there’s still a ton of bugs and I know this can be written better and I need some help.

We’re trying to create a system for a game’s lobby where players will join a queue to get into the main game. Currently, this is achieved by having players walk into a ship, collide with an invisible barrier part, and then get placed into a seat on the ship. Then, once there are enough players sitting in the ship, a countdown timer initiates. Once it reaches zero all the players in the ship are sent to the main game via teleport service. There are also some checks to teleport players back outside the ship when they exit their seats as well as stopping or resetting the countdown when players leave, but they don’t seem to be working much at all.

This does sort of work some of the time, but I often run into a ton of bugs and glitches that need to be addressed. I’ve listed some of the main issues below.

  • When touching teleport part, sometimes player teleports back and forth between seat and outside.
  • Sometimes countdown timer decreases in increments larger than 1.
  • When getting out of the seat, sometimes the player is not teleported back outside.
    • When touching the teleport part from inside the ship, sometimes the player rapidly teleports outside then back in the ship.

Ideas for fixes

  • Created a debounce/cooldown for collision checks. (I’ve created the debounce variable for this but haven’t really utilized it yet)
  • Clean up the code that teleports the player outside when getting out of a seat and sets the “insideShip” property to true or false

Here’s a screenshot of the ship and TeleportPart(Barrier)

And here’s the teleport script itself:

-- Creating seats table --
local seats = {}
for I, part in game.Workspace.Spaceship:GetChildren() do
	if part.Name == 'Seat'then
		table.insert(seats,part)
	end
end

-- Defining Variables --
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local barrier = script.Parent

local debounce = false
local playerCount = ReplicatedStorage.PlayerCount
local countdown = ReplicatedStorage.Countdown
local timer = ReplicatedStorage.CountdownTimer

-- Functions --
barrier.Touched:Connect(function(otherpart) -- On touch function 
	local character = otherpart:FindFirstAncestorWhichIsA("Model") -- Character and player checks
	if not character then return end
	local player = game.Players:GetPlayerFromCharacter(character)
	if not player then return end
	
	local insideShip = player:FindFirstChild("insideShip") -- Create/find "insideShip" on player
	if not insideShip then
		insideShip = Instance.new("BoolValue")
		insideShip.Name = "insideShip"
		insideShip.Parent = player
	end
	if insideShip.Value == false and playerCount.Value < 10 then
		for i , seat in seats do
			if seat.Occupant == nil then
				insideShip.Value = true
				character:FindFirstChild('HumanoidRootPart').Position = seat.Position
				playerCount.Value += 1
				break
			end
		end
	else
		insideShip.Value = false
		playerCount.Value -= 1
		character:FindFirstChild("HumanoidRootPart").Position = barrier.Position - Vector3.new(0,0,10)
	end
	if playerCount.Value > 0 then
		countdown.Value = true
		print("Countdown initiated")
	else
		countdown.Value = false
		print("Countdown cancelled")
		timer.Value = 30
	end
end)

countdown:GetPropertyChangedSignal("Value"):Connect(function()
	if countdown.Value == false then return end
	if countdown.Value == true then
		while countdown.Value == true do
			print("Countdown is true")
			timer.Value -= 1
			wait(1)
			print(timer.Value)
			if(timer.Value <= 0) then
				playerCount.Value = 0
				print("teleporting")
				for i, player in game.Players:GetPlayers() do
					if player.insideShip.Value == true then
						game["Teleport Service"]:Teleport(13477484229, player)
					end
				end
				countdown.Value = false
				timer.Value = 30
			end
		end
	end
end)

There are also a few objects located in Replicated storage I’m using for this.

  • Countdown
    • BoolValue, set to False by default
  • CountdownTimer
    • NumberValue, set to 30 by default
  • PlayerCount
    • NumberValue, set to 0 by default

I could probably just do it all in the script, but idk if that works better or worse than what I have now. Let me know if I should change it.

Lastly, here’s a project file that contains the main teleport script and all the important models and parts:
Teleport to different game student example.rbxl (96.2 KB)

I’m pretty sure this can be done better, but I don’t really know where to start. Thanks for the help!

1 Like

I believe the reason for the timer decreasing with more than 1 could be because of the last segment. Every time countdown.Value is set to true, it will start a countdown. For it to stop counting, countdown.Value needs to be false. However, if the timer stops and starts before the current timer would have counted a second, I believe it would run two timers at once. The reason for this is because every time countdown.Value is set to true, it will start a timer, but it will only stop if countdown.Value is false at the beginning of each second.

So should I do this instead?

countdown:GetPropertyChangedSignal("Value"):Connect(function()
	if countdown.Value == false then return end
	if countdown.Value == true then
		wait(1)
		while countdown.Value == true do
			print("Countdown is true")
			timer.Value -= 1
			print(timer.Value)