Teleport Script not Working!

Hey Developers!
I am making a story game, currently the lobby, I tested it, but It didn’t work as it should, me and
@EconomicCrash47 tested it. He got in the bus, I waited to see, he got teleported, and after a few second, the time went back to counting down, but the amount of players didn’t Im guessing it’s a problem with my PlayerRemoving feature. I get no errors. A screenshot shows that the amount of players doesn’t reset:

Shouldn’t the player removing function work?

Here is my script:

--Variables
local Bus = game.Workspace:WaitForChild('Bus')
local Seats = Bus.Seats
local Hitbox = game.Workspace:WaitForChild('Hitbox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TeleportService = game:GetService('TeleportService')
local ServerStorage = game:GetService('ServerStorage')
local Status = ReplicatedStorage:WaitForChild('Status')
local UI = ServerStorage:WaitForChild('ExitGui')
local BusPlayers = ReplicatedStorage:WaitForChild('BusPlayers')
local TeleportingValue = ReplicatedStorage:WaitForChild('Teleporting')

local MinimumPlayers = 1
local Time = 30
local Teleporting = false
local BusTable = {}
local TeleportPlaceId = 4829097735

local Code = TeleportService:ReserveServer(TeleportPlaceId)

Hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and Teleporting == false then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if table.find(BusTable, Player) == nil then
			for _, seat in pairs(Seats:GetChildren()) do
				if seat.Occupant == nil then
					table.insert(BusTable, Player)
					local Humanoid = Player.Character.Humanoid
					Humanoid.JumpPower = 0
					seat:Sit(Humanoid)
					UI:Clone().Parent = Player.PlayerGui
					BusPlayers.Value = #BusTable --see here
					break
				end
			end
		end
	end
end)

ReplicatedStorage.LeaveBus.OnServerEvent:Connect(function(Player)
	local ReturnValue = table.find(BusTable, Player)
	table.remove(BusTable, ReturnValue)
	warn('Removed '..ReturnValue..' from the BusTable!')
	BusPlayers.Value = #BusTable --see here
	Player:LoadCharacter()
	Player.Character:WaitForChild('HumanoidRootPart').CFrame = game.Workspace.Setback.CFrame
end)

while true do
	wait()
	for i = Time, 1, -1 do
		Status.Value = i
		wait(1)
		local Players = #BusTable --see here
		if i == 1 and Players >= MinimumPlayers then
			--Teleport the Players!
			Teleporting = true
			TeleportingValue.Value = true
			for _, Player in ipairs(BusTable) do
				local FadeGui = ServerStorage.FadeGui:Clone()
				FadeGui.Parent = Player.PlayerGui
			end
			wait()
			TeleportService:TeleportToPrivateServer(TeleportPlaceId, Code, BusTable)
			wait(6)
			Teleporting = false
			TeleportingValue.Value = false
		elseif i == 1 and Players < MinimumPlayers then
			Status.Value =  MinimumPlayers..' Players Needed!'
			wait(1)
		elseif Players == 12 then
			--Also teleport the players!
		end
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	print(Player..' left!')
	if table.find(BusTable, Player) ~= nil then
	    local ReturnValue = table.find(BusTable, Player)
		table.remove(BusTable, ReturnValue)
		BusPlayers.Value = #BusTable
		warn(BusPlayers)
		warn(Player.Name.. 'has left the game, and has been removed from the BusTable')
	end
end)

When he got teleported, There was no message saying he left, which should have been printed when he left.
Thanks for help!

4 Likes

You put your PlayerRemoving function under a while true do loop. These will yield the entire script after it, so you can either move PlayerRemoving above the loop, or add the loop in a coroutine.wrap to prevent yielding.

I didn’t!
The Player Removing function isn’t in a while loop. It is after the end.

1 Like

Yes, that’s the problem. The while loop will prevent all code after it from running, unless you add it in a coroutine.

1 Like

Can you tell me how to add it in a coroutine?

1 Like
coroutine.wrap(function()
	while true do
		wait()
		for i = Time, 1, -1 do
			Status.Value = i
			wait(1)
			local Players = #BusTable --see here
			if i == 1 and Players >= MinimumPlayers then
				--Teleport the Players!
				Teleporting = true
				TeleportingValue.Value = true
				for _, Player in ipairs(BusTable) do
					local FadeGui = ServerStorage.FadeGui:Clone()
					FadeGui.Parent = Player.PlayerGui
				end
				wait()
				TeleportService:TeleportToPrivateServer(TeleportPlaceId, Code, BusTable)
				wait(6)
				Teleporting = false
				TeleportingValue.Value = false
			elseif i == 1 and Players < MinimumPlayers then
				Status.Value =  MinimumPlayers..' Players Needed!'
				wait(1)
			elseif Players == 12 then
				--Also teleport the players!
			end
		end
	end
end)()
2 Likes