Countdown Not Working

I’m making a lobby teleporting system. I have a SurfaceGui to show 15 second countdown until teleportation. When the player leaves, it should be Disabled and paused. When the player joins back, the countdown should resume as normally.

When I touch the part to join the queue, the countdown Enables and works well. But when I leave and try to join again, I have a few issues- the countdown either countinues, doesn’t disappear, or doesn’t reset back to 15, or even a mix of all three.

Here is a video of my problem when I hit play: link

I have tried looking through the DevForum for a way to make my while true do loop to stop when a certain value is changed, which could stop the countdown but I haven’t been able to find anything and am not even sure if that’s the right solution to my problems.

Here is my Main Script. Scroll down to the area I have commented on so it’s easier for you to see where the problem is located. (Server Script)

--general
local door = script.Parent
local MaxPlrs = 0
local teleportPart = door.teleportPart
--events
local rps = game:GetService("ReplicatedStorage")
local camEvent = rps:WaitForChild("CameraEvent")
--telporting
local playersTelporting = {}
local tps = game:GetService("TeleportService")
local placeId = 12843002487
local loadgui = game.StarterGui.LoadGui
local teleported = false
local tpevent = rps.TeleportEvent
--camera
local camera = game.Workspace.Camera
local campart = door.cameraFocus
--leave lobby
local leavegui = game.StarterGui.leave
local playerNamesTp = {}
local Players = game:GetService("Players")
local leaveEvent = rps.LeaveEvent
local teleportout = door:WaitForChild("teleportOut")
-- others
local debounce = false
local finished = false
--countdown
local countdown = 15
local countdowngui = door.SurfaceGui.time
local numplrgui = door.billboardPart.BillboardGui.players
local alldoorgui = door.SurfaceGui
countdowngui.Visible = false

door.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") and  MaxPlrs < 7 then
		if not debounce then
			debounce = true
		
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if plr then
			
			countdowngui.Visible = true
			countdown = 15
			countdowngui.Text = (countdown)
			MaxPlrs += 1
			print(MaxPlrs)
			
			
			
			if playersTelporting[plr] then
				print(plr.Name.." joined.")
			else
				playersTelporting[plr] = true
				table.insert(playersTelporting, plr)
				wait()
					print(plr.Name.." joined.")
			end
			
			plr = plr
			
			local hmr = hit.Parent.HumanoidRootPart
			
			hit.Parent.HumanoidRootPart.CFrame = teleportPart.CFrame
			
			camEvent:FireClient(plr, teleported, playersTelporting, placeId, loadgui, leavegui, MaxPlrs, finished, hmr, teleportout)
			
			tpevent:FireClient(plr, teleported, playersTelporting, placeId, loadgui)
			
			leaveEvent.OnServerEvent:Connect(function(player, playerLeft)
				MaxPlrs -= 1
				while true do
					if MaxPlrs == 0 then
						print("Everyone left.")
						countdowngui.Visible = false
						countdown = 15
						countdown = 15
						finished = false
						print(countdown)
						countdown = 15
						
						hmr.CFrame = teleportout.CFrame
						
						return
					end
					
					wait()
				end
				finished = false
				print(finished)
				countdown = nil
				local leavePart = door:FindFirstChild("teleportOut")
				hmr.CFrame = leavePart.CFrame
				print(countdown)
				print(playersTelporting)
				
			end)

		end
	debounce = false
			
		end
	else return		
	end
finished = true
end)

repeat wait() until finished == true

while true do
	wait()
	
	if finished == true then
		print(countdown)
		while finished == true and countdown ~= 0 do --here is the while true loop. I tried adding another requirement in hopes it could stop it, but it doesn't work the way it's meant to.
			wait(1)
			countdown -= 1 
			countdowngui.Text = (countdown)
			print(countdown)
		end
		
		if finished == true and MaxPlrs~= 0 then
		table.clone(playersTelporting)
		for _, plr in ipairs(playersTelporting) do
			local newGui = loadgui:Clone()
			newGui.Parent = plr.PlayerGui
			newGui.Enabled = true

			wait(3)

				tps:SetTeleportGui(loadgui)
				local accessCode = tps:ReserveServer(placeId)
				tps:TeleportToPrivateServer(placeId, accessCode, playersTelporting, "SpawnPoint", countdown, loadgui)
				print("got tp")
				playersTelporting[plr] = nil 
				table:clear(playersTelporting)
				countdown = 15
				MaxPlrs = 0
			end

			return
		
		end
	end
	
	if finished ~= true then
		countdown = 15
	end
	
end

Any help is appreciated! If you have any questions so that you can better help me, feel free to ask. Thanks in advance!

You need to ensure that the countdown behaves correctly when players leave and rejoin:

  • You should be using a flag to indicate whether the countdown is active or paused.
  • Make sure the countdown resets properly when all players leave.
  • Resume the countdown from where it left off when a player rejoins.

Try this and let me know if it works. This script should ensure that the countdown behaves correctly, pausing when players leave and resuming or restarting as needed.

local door = script.Parent
local MaxPlrs = 0
local teleportPart = door.teleportPart
local rps = game:GetService("ReplicatedStorage")
local camEvent = rps:WaitForChild("CameraEvent")
local playersTelporting = {}
local tps = game:GetService("TeleportService")
local placeId = 12843002487
local loadgui = game.StarterGui.LoadGui
local tpevent = rps.TeleportEvent
local camera = game.Workspace.Camera
local campart = door.cameraFocus
local leavegui = game.StarterGui.leave
local playerNamesTp = {}
local Players = game:GetService("Players")
local leaveEvent = rps.LeaveEvent
local teleportout = door:WaitForChild("teleportOut")
local debounce = false
local countdown = 15
local countdowngui = door.SurfaceGui.time
local numplrgui = door.billboardPart.BillboardGui.players
local alldoorgui = door.SurfaceGui
countdowngui.Visible = false
local countdownActive = false -- New flag to manage countdown state

local function startCountdown()
    countdownActive = true
    while countdownActive and countdown > 0 do
        wait(1)
        countdown -= 1
        countdowngui.Text = tostring(countdown)
        print(countdown)
    end
    if countdown == 0 and MaxPlrs > 0 then
        teleportPlayers()
    end
end

local function resetCountdown()
    countdownActive = false
    countdown = 15
    countdowngui.Text = tostring(countdown)
end

local function teleportPlayers()
    local playersToTeleport = {}
    for _, plr in pairs(playersTelporting) do
        table.insert(playersToTeleport, plr)
    end
    local accessCode = tps:ReserveServer(placeId)
    tps:TeleportToPrivateServer(placeId, accessCode, playersToTeleport, "SpawnPoint", countdown, loadgui)
    print("got tp")
    playersTelporting = {}
    MaxPlrs = 0
    resetCountdown()
end

door.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and MaxPlrs < 7 then
        if not debounce then
            debounce = true
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            if plr then
                countdowngui.Visible = true
                if not playersTelporting[plr] then
                    playersTelporting[plr] = true
                    MaxPlrs += 1
                    print(plr.Name .. " joined.")
                    if MaxPlrs == 1 then
                        startCountdown()
                    end
                end
                plr.Character.HumanoidRootPart.CFrame = teleportPart.CFrame
                camEvent:FireClient(plr, false, playersTelporting, placeId, loadgui, leavegui, MaxPlrs, false, plr.Character.HumanoidRootPart, teleportout)
                tpevent:FireClient(plr, false, playersTelporting, placeId, loadgui)
                debounce = false
            end
        end
    end
end)

leaveEvent.OnServerEvent:Connect(function(player, playerLeft)
    if playersTelporting[player] then
        playersTelporting[player] = nil
        MaxPlrs -= 1
        print(player.Name .. " left.")
        if MaxPlrs == 0 then
            resetCountdown()
            countdowngui.Visible = false
        end
    end
end)
1 Like

I put the script in, but the player isn’t teleported inside of the lobby area, which doesn’t allow my LeaveGui in my local script to enable. I’ll mess around with it a bit to see if I can make it work. Thank you!

Is the LeaveGui already stored in PlayerGui?

Yes- when the player touches the door, an event should be fired to a local script to enable it after the player’s camera position is changed. Would you like me to send the local script?

1 Like

Sure, send the Local Script and we’ll see what we can do to fix it.

Here is the local script. I’m assuming it’s not working because you changed a few parameters in my main script. I’ll switch it back and see if that works then. Thank you!

local MaxPlayers = nil
local done = nil
local hmr = nil
local teleportout = nil

finished = false


camEvent.OnClientEvent:Connect(function(tpd, plrsInLobby, placeId, loadgui, leaveGui, mxplrs, fin, humpart, tpout)
	MaxPlayers = mxplrs
	teleported = tpd
	playersTeleporting = plrsInLobby
	placeid = placeId
	leavegui = leaveGui
	done = fin
	hmr = humpart
	teleportout = tpout

	camera.CameraType = Enum.CameraType.Scriptable
	local camera = workspace.CurrentCamera
	local focus = campart
	repeat
		wait()
		camera.CameraType = Enum.CameraType.Scriptable
	until camera.CameraType == Enum.CameraType.Scriptable
	camera.CFrame = focus.CFrame

	finished = true
	
	repeat task.wait() until finished == true

	table.clone(playersTeleporting)
	for _, plr in ipairs(playersTeleporting) do
		
		local leavegui = plr.PlayerGui.leave
		leavebtn = leavegui.leave.TextButton
		leavegui.Enabled = true
		
		
	end
	
	leavebtn.MouseButton1Click:Connect(function(player)
		leavebtn.Parent.Parent.Enabled = false
		wait()
		table.remove(playersTeleporting, player)
		camera.CFrame = leavecam.CFrame
		camera.CameraType = Enum.CameraType.Custom
		playerLeft = true
		finished = false
		done = false
		hmr.CFrame = teleportout.CFrame
		leftEvent:FireServer(playerLeft)

Is this the full script? Does it send any errors? There seems to be some unfinished elements?

This is the full script. There are no errors when I hit play. The only issue is in the Main Script, due to the player not being teleported in, camera position not changing, and the leave button not appearing

Would you be able to send the place file? It’ll be easier to debug then.

1 Like

Sorry, I don’t know how to do that :sweat_smile: could you please explain how?

reversescriptinghelp.rbxl (159.2 KB)

I think I figured it out. Let me know if this works!

@JamesBlossoms Sorry to disturb you, but I’ve sent the file. Have you been able to figure out the reason?

1 Like

Here you go. You will need to tweak it further to meet your needs, but I removed a bunch of redundant code and it should be working as expected.

reversescriptinghelp.rbxl (156.8 KB)

2 Likes

Thank you for your help! Your time is appreciated.

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