Help On Game Teleport

Me and my friend were trying to do a game teleporter. We spent one hour trying to make it work! It teleports but, I need it to kick the player if they click the leave button. It kicks the player but it kicks both of us out. I need help!

local playerCount = 0
local playersTable = {}
local teleporting = false
local tpservice = game:GetService("TeleportService")
local rs = game:GetService("ReplicatedStorage")
local Enter = rs:WaitForChild("Enter")
local Leave = rs:WaitForChild("Leave")
local placeID = 15668447936
local players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit) 

	if hit.Parent:FindFirstChild("Humanoid") then
		local Char = players:GetPlayerFromCharacter(hit.Parent)
		
	
--print("Fired")
		if teleporting == false then
--print("False")
			if table.find(playersTable, Char) then return end
			print("plr")
			Enter:FireClient(Char)
			print("Event")
			playerCount = playerCount + 1
			
		local PlrInList = Instance.new("NumberValue") -- assigns number
		PlrInList.Value = playerCount -- plr assigned number
		PlrInList.Parent = Char

		print(PlrInList.Value)
		print(playerCount)
		
			script.Parent.Parent.SurfaceGui.TextLabel.Text = (playerCount .. "/4")

			table.insert(playersTable, Char)
			
			local rootPart = hit.Parent:WaitForChild("HumanoidRootPart")
			 
			Leave.OnServerEvent:Connect(function()
				table.remove(playersTable, PlrInList.Value) -- will kick the player depending on their number
				rootPart.Position = script.Parent.Parent["Exit Part"].Position
				hit.Parent:WaitForChild("Humanoid").WalkSpeed = 16
				playerCount -= 1 -- decreased by 1 once the player leaves
				print(playerCount) -- goes to 0 as intended but then it goes -1,-2,-3
				PlrInList:Destroy()
				if playerCount == 0 then
					teleporting = false
				end
			end)
			
			
			rootPart.Position = script.Parent.Parent["TP part"].Position
			hit.Parent:WaitForChild("Humanoid").WalkSpeed = 0
	if teleporting == true then
			wait(5)

			print(playersTable)
			local resServer = tpservice:ReserveServer(placeID)

			teleporting = true
			tpservice:TeleportToPrivateServer(placeID, resServer, playersTable)
			
			playerCount = 0
			table.clear(playersTable)
		end
	end
	end
end)



1 Like

Fixed it!!!

local TS = game:GetService("TeleportService")
local placeId = 15668447936
local Leave = game.ReplicatedStorage:WaitForChild("Leave")
local Enter = game.ReplicatedStorage:WaitForChild("Enter")
local PlrList = {}
local timer
local spawnTeleport = script.Parent.Parent["TP part"]
local SurfaceGui = script.Parent.Parent.SurfaceGui
local Billboard = spawnTeleport.Timer
local teleporting = false

local MaxPlayer = 4 -- How many players

local function UpdatePlayerText()
	SurfaceGui.TextLabel.Text = "Players: " ..#PlrList.. "/"..MaxPlayer
end

local function RemovePlayer(character)
	for i=1,#PlrList do
		if PlrList[i] == character.Name then
			table.remove(PlrList,i)
			UpdatePlayerText()
		end
	end
end

local function teleportPlayers()
	if #PlrList > 0 then
		SurfaceGui.TextLabel.Text = "TELEPORTING"
		local playersToTeleport = {}
		local teleportTime = 0
		
		for i=1,#PlrList do
			if game.Players:FindFirstChild(PlrList[i]) then
				table.insert(playersToTeleport, game.Players:FindFirstChild(PlrList[i]))
				Enter:FireClient(game.Players:FindFirstChild(PlrList[i]))
			else
				table.remove(PlrList,i)	
			end
		end 
		
		local Server_Code = TS:ReserveServer(placeId)
		teleporting = true
		
		TS:TeleportToPrivateServer(placeId,Server_Code,playersToTeleport)
		repeat wait() until #PlrList <= 0
		teleporting = false
	end
end

script.Parent.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local RootPart = char:FindFirstChild("HumanoidRootPart")
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false
			
			Enter:FireClient(player)
			
			for i=1,#PlrList do
				if PlrList[i] == char.Name then
					alreadyExists = true
				end
			end

			if alreadyExists == false then
				if #PlrList < MaxPlayer then -- Many Players have been teleported.
					table.insert(PlrList,char.Name)
					RootPart.Position = spawnTeleport.Position
					print("Teleported")
					UpdatePlayerText()
					Leave:FireClient(player)
				end

				player.CharacterRemoving:connect(function(character)
					RemovePlayer(character)
				end)
			end

		end
	end
end)

Leave.OnServerEvent:Connect(function(player)
	if player.Character then
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		player.Character:MoveTo(script.Parent.Parent["Exit Part"].Position)
		RemovePlayer(player.Character)
	end
end)

while wait() do
	timer = 16
	for i=1,timer do
		timer -= 1
		Billboard.TimerLabel.Text = timer
		wait(1)
	end
	teleportPlayers()
end

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