Help with timer

I’m trying to make a timer player limiter teleporter

but the timer is glitchy

local ts = game:GetService('TeleportService')
local PlaceId = 9479160887
local maxPlayers = 16
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Folder = script.Parent:WaitForChild('Players')
local intermissionLength = 30
local Players = game:GetService('Players')
local remote1 = ReplicatedStorage:WaitForChild('Remotes'):WaitForChild('JoinLobby')
local remote2 = ReplicatedStorage:WaitForChild('Remotes'):WaitForChild('LeaveLobby')
local node = script.Parent.Parent:WaitForChild('TeleportNode')
local node2 = script.Parent.Parent:WaitForChild('TeleportNode2')
local playerCount = script.Parent:WaitForChild('billboard'):WaitForChild('SurfaceGui'):WaitForChild('TextLabel')
local timerText = script.Parent:WaitForChild('billboard'):WaitForChild('timer'):WaitForChild('TextLabel')
local debouncer = false

function checkIfExists(Character) -- checks if player exists
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value == Character then
			return true
		end
	end
	return false

end

function teleportParty()
	local Plrs = {}
	for _,value in pairs(Folder:GetChildren()) do 
		if value then
			table.insert(Plrs,Players:GetPlayerFromCharacter(value.Value))
		end
	end
	local Code = ts:ReserveServer(PlaceId)
	ts:TeleportToPrivateServer(PlaceId,Code,Plrs)
	Folder:ClearAllChildren()	
end

function timer()
	intermissionLength = 30
	while wait() do
		for i = intermissionLength,0,-1 do
			timerText.Visible = true
			wait(1)
			intermissionLength =  i
			timerText.Text = i
			if #Folder:GetChildren() == 0   then
				intermissionLength = 30
				return
			end
		end
		if #Folder:GetChildren() >= maxPlayers or intermissionLength == 0 then
			teleportParty()

		end
	end
end


function UpdateGui()
	playerCount.Text = tostring(#Folder:GetChildren())..'/'..tostring(maxPlayers)
end


script.Parent.Touched:Connect(function(part)
		if part.Parent:FindFirstChild('Humanoid') then
			local player = part.Parent
			if not checkIfExists(player) then
				local Tag = Instance.new('ObjectValue')
				Tag.Value = part.Parent
				Tag.Parent = Folder
				part.Parent:SetPrimaryPartCFrame(node.CFrame)
				remote1:FireClient(Players:GetPlayerFromCharacter(part.Parent))
				end
		if #Folder:GetChildren() > 0 then
			timer()
		end
	end
end)

remote2.OnServerEvent:Connect(function(player)
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value == player.Character then
			value:Destroy()
			player.Character:SetPrimaryPartCFrame(node2.CFrame)
		end
	end

end)



Folder.ChildAdded:Connect(UpdateGui)
Folder.ChildRemoved:Connect(UpdateGui)

any help would be great

2 Likes
local ts = game:GetService('TeleportService')
local PlaceId = 9479160887
local maxPlayers = 16
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Folder = script.Parent:WaitForChild('Players')
local intermissionLength = 30
local Players = game:GetService('Players')
local remote1 = ReplicatedStorage:WaitForChild('Remotes'):WaitForChild('JoinLobby')
local remote2 = ReplicatedStorage:WaitForChild('Remotes'):WaitForChild('LeaveLobby')
local node = script.Parent.Parent:WaitForChild('TeleportNode')
local node2 = script.Parent.Parent:WaitForChild('TeleportNode2')
local playerCount = script.Parent:WaitForChild('billboard'):WaitForChild('SurfaceGui'):WaitForChild('TextLabel')
local timerText = script.Parent:WaitForChild('billboard'):WaitForChild('timer'):WaitForChild('TextLabel')
local debouncer = false

local TimerActive = false

function checkIfExists(Character) -- checks if player exists
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value == Character then
			return true
		end
	end
	return false

end

function teleportParty()
	local Plrs = {}
	for _,value in pairs(Folder:GetChildren()) do 
		if value then
			table.insert(Plrs,Players:GetPlayerFromCharacter(value.Value))
		end
	end
	local Code = ts:ReserveServer(PlaceId)
	ts:TeleportToPrivateServer(PlaceId,Code,Plrs)
	Folder:ClearAllChildren()	
end

function timer()
    if not TimerActive then
    TimerActive = true
	intermissionLength = 30
	while wait() do
		for i = intermissionLength,0,-1 do
			timerText.Visible = true
			wait(1)
			intermissionLength =  i
			timerText.Text = i
			if #Folder:GetChildren() == 0   then
				intermissionLength = 30
				return
			end
		end
		if #Folder:GetChildren() >= maxPlayers or intermissionLength == 0 then
			TimerActive = false
teleportParty()
            

		end
	end
end
end


function UpdateGui()
	playerCount.Text = tostring(#Folder:GetChildren())..'/'..tostring(maxPlayers)
end


script.Parent.Touched:Connect(function(part)
		if part.Parent:FindFirstChild('Humanoid') then
			local player = part.Parent
			if not checkIfExists(player) then
				local Tag = Instance.new('ObjectValue')
				Tag.Value = part.Parent
				Tag.Parent = Folder
				part.Parent:SetPrimaryPartCFrame(node.CFrame)
				remote1:FireClient(Players:GetPlayerFromCharacter(part.Parent))
				end
		if #Folder:GetChildren() > 0 then
			timer()
		end
	end
end)

remote2.OnServerEvent:Connect(function(player)
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value == player.Character then
			value:Destroy()
			player.Character:SetPrimaryPartCFrame(node2.CFrame)
		end
	end

end)



Folder.ChildAdded:Connect(UpdateGui)
Folder.ChildRemoved:Connect(UpdateGui)

2 Likes