Teleport script not reacting

hello. this script doesnt react if i trigger the prompt to fire the “EXIT” function. it also doesnt work with any other function. what is making the script get stuck? the teleporting works however.

--SETUP
script.Player_Limit.Value = script.MAIN_MODEL.Value:GetAttribute("PLR_LIMIT")
script.PLACE_ID.Value = script.MAIN_MODEL.Value:GetAttribute("PLACE")



local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = script.PLACE_ID.Value
local teleporting = false
local spawnTeleport = script.ENTER_Pos.Value
local Leave_TP = script.LEAVE_PART.Value

script.Gui.Value.Frame.players.Text = "Players: " ..#script.People:GetChildren().. "/"..script.Player_Limit.Value..""
script.Gui.Value.Frame.CountDown.Text = script.CountDownTime.Value



local function updateGui()
	script.Gui.Value.Frame.players.Text = "Players: " ..#script.People:GetChildren().. "/"..script.Player_Limit.Value..""
end

local function removeFromList(Character)
	if script.People:FindFirstChild(game.Players:GetPlayerFromCharacter(Character).Name) then
		script.People:FindFirstChild(game.Players:GetPlayerFromCharacter(Character).Name):Destroy()
		updateGui()
	end
end

function Teleport_Players()

	script.EVENTS.FADE_IN:FireAllClients()

	wait(0.2)

	local PlayerTable = {}

	for a, b in ipairs(script.People:GetChildren()) do
		table.insert(PlayerTable, b.Value)
	end
	--local ServerCode = game["Teleport Service"]:ReserveServer(placeId)
	--game["Teleport Service"]:TeleportToPrivateServer(placeId,ServerCode,PlayerTable)
	game.TweenService:Create(script.CAM_PARTS.MOVING.Value,TweenInfo.new(4,Enum.EasingStyle.Sine),{CFrame = script.CAM_PARTS.POS_TWO.Value.CFrame}):Play()
	wait(4)
	script.CAM_PARTS.MOVING.Value.CFrame = script.CAM_PARTS.POS_ONE.Value.CFrame
	script.EVENTS.DOORS:Fire("OPEN")
	for a, b in ipairs(script.People:GetChildren()) do
		b:Destroy()
	end
	table.clear(PlayerTable) --reset the table!
	script.Gui.Value.Frame.Status.Text = "READY"
	updateGui()
	script.EVENTS.DOORS:Fire("OPEN")
end

local Enter_db = true
script.ENTER_PART.Value.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then


		local char = hit.Parent
		local alreadyExists = false
		if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not script.People:FindFirstChild(""..player.Name.."") then
			if #script.People:GetChildren() < script.Player_Limit.Value then
				local Val = Instance.new("ObjectValue",script.People)
				Val.Name = player.Name
				Val.Value = player

				local GUI = script.SCENE_GUI.Value:Clone()
				GUI.Parent = player.PlayerGui

				char:PivotTo(script.ENTER_Pos.Value.CFrame)
				updateGui()
				if #script.People:GetChildren() == script.Player_Limit.Value then
					script.EVENTS.DOORS:Fire("CLOSE")
					wait(0.2)
					Teleport_Players()
				end

				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
					end)
					end
			end
		end
	end
	wait(0.2)
end)

--CountDown
local CountDownTime = script.CountDownTime.Value
local Counting = false
local CountDownLabel = script.Parent.Parent.WALLS.iNVISIBLE_WALL.GUI.Frame.CountDown

local CountDown = CountDownTime

--Sets Counting to false or true depending if there are playsers
script.People.ChildAdded:Connect(function()
	Counting = true
end)
script.People.ChildRemoved:Connect(function()
	if #script.People:GetChildren() ~= 0  then return end--set counting to false if no player
	Counting = false

	--Reset countdown
	CountDown = CountDownTime
	CountDownLabel.Text = CountDown
end)
--Sets Counting to false or true depending if there are playsers
	while true do
		wait(1)
		if not Counting or #script.People:GetChildren() == 0 then CountDown = CountDownTime end
		script.TICK_SOUND.Value:Play()

		CountDown = CountDown -1
		CountDownLabel.Text = CountDown
		updateGui()
		if CountDown == 0 then
			CountDown = CountDownTime
			Teleport_Players()
		end
	end



function EXIT(player)
	warn("searching char")
	if player.Character then
warn("attempt to leave")
		if player.PlayerGui:FindFirstChild(script.SCENE_GUI.Value.Name) then
			player.PlayerGui:FindFirstChild(script.SCENE_GUI.Value.Name):Destroy()
		end

		player.Character:PivotTo(script.LEAVE_PART.Value.CFrame)
		removeFromList(player.Character)
	end
end

script.Prompt.Value.Triggered:Connect(EXIT)
4 Likes

I think it’s getting stuck because of the infinite loop at the end, aparently the while loop will continue running indefinitely without any breaks, and it prevents the script from reaching the script.Prompt.Value.Triggered:Connect(EXIT) line.

Try it:

-- all the code before

-- Countdown function
local function StartCountdown()
	local CountDownTime = script.CountDownTime.Value
	local Counting = false
	local CountDownLabel = script.Parent.Parent.WALLS.iNVISIBLE_WALL.GUI.Frame.CountDown
	local CountDown = CountDownTime

	-- Sets Counting to false or true depending if there are players
	script.People.ChildAdded:Connect(function()
		Counting = true
	end)
	script.People.ChildRemoved:Connect(function()
		if #script.People:GetChildren() ~= 0 then return end
		Counting = false

		-- Reset countdown
		CountDown = CountDownTime
		CountDownLabel.Text = CountDown
	end)

	while true do
		wait(1)
		if not Counting or #script.People:GetChildren() == 0 then
			CountDown = CountDownTime
		end

		script.TICK_SOUND.Value:Play()

		CountDown = CountDown - 1
		CountDownLabel.Text = CountDown
		updateGui()

		if CountDown == 0 then
			CountDown = CountDownTime
			Teleport_Players()
		end
	end
end

-- Start the countdown loop
StartCountdown()

function EXIT(player)
	-- your code
end

script.Prompt.Value.Triggered:Connect(EXIT)

This still won’t work because StartCountdown is called before the Triggered is connected, and the while loop will cause it to loop indefinitely. It should be like so:

function EXIT(player)
	-- your code
end

script.Prompt.Value.Triggered:Connect(EXIT)

-- Start the countdown loop
StartCountdown()

Alternatively, you could call StartCountdown using task.spawn or coroutine.wrap.

1 Like

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