Elevator Time Count Is Bug

Hi Im CTG I Need Help With My Script, Im Making A Elevator TDS But It Bug Time, Sometime When Time Count Is 4 Or 27(It Random) It Will Teleport, I Want It When Count To 0 It Will Tp

Here Is My Script

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = script.Parent.MapChoose.Value
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local gui = script.Parent.GuiPart.SurfaceGui
local billboard = script.Parent.billboardPart.billboardGui
local timer = script.Parent.Time
local teleporting = false
local spawnTeleport = script.Parent.spawn

local function teleportPlayers()
	if #list > 0 then
		script.Parent.billboardPart.billboardGui.Frame.time.Text = "Player Need"
		local playersToTeleport = {}
		local teleportTime = 0
		for i=1,#list do
			if game.Players:findFirstChild(list[i]) then
				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
			else
				table.remove(list,i)	
			end
		end 
		local code = TS:ReserveServer(placeId)
		teleporting = true
		TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
		repeat wait() until #list <= 0
		script.Parent.billboardPart.billboardGui.Frame.time.Text = "Player Need"
		teleporting = false
	end
end


local function updateGui()		
	gui.Frame.players.Text = #list .. " / 4" 
	
	if #list == 1 then
		gui.Frame.playerFrame:TweenSize(UDim2.new(0.25, 0 ,0.2, 0))
		billboard.Frame.time.Visible = true
		billboard.Frame.PlayerNeed.Visible = false
		timer.Value = 30
	end
	
	if #list == 2 then
		gui.Frame.playerFrame:TweenSize(UDim2.new(0.5, 0 ,0.2, 0))
	end
	
	if #list == 3 then
		gui.Frame.playerFrame:TweenSize(UDim2.new(0.75, 0 ,0.2, 0))
	end
	
	if #list == 4 then
		gui.Frame.playerFrame:TweenSize(UDim2.new(1, 0 ,0.2, 0))
	end
	
	if #list == 0 then
		timer.Value = 30
		billboard.Frame.time.Visible = false
		billboard.Frame.PlayerNeed.Visible = true
		gui.Frame.playerFrame:TweenSize(UDim2.new(0, 0 ,0.2, 0))
	end
	
end

local function removeFromList(character)
	for i=1,#list do
		if list[i] == character.Name then
			table.remove(list,i)
			updateGui()
		end
	end
end

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false
			
			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end
			
			if alreadyExists == false then
				if #list < 4 then
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end
				
				player.CharacterRemoving:connect(function(character)
				removeFromList(character)
				end)
			end
			
		end
	end
end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
	if player.Character then
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		removeFromList(player.Character)
		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
	end
end)

while wait() do
	print("2")
	for time1=0 ,timer.Value do
		timer.Value -= 1
		billboard.Frame.time.Text = timer.Value
		wait(1)
	end
	timer.Value = 30 
	teleportPlayers()
	wait(3)
end
1 Like

For anyone looking for more information about what CTG wants here is their original post from a few minutes ago: How To Make A Elevator Like TDS