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!