How to have elevator teleport everyone in the elevator

How would I be able to have it so that the elevator checks for the amount of torsos in the elevator, and then teleports them all into the other elevator, and not just the player who pressed the button? I am not sure how to do this.

Door1.Button2.ClickDetector.MouseClick:connect(function(Player) -- Player who pressed the button will only teleport
	if not debounce then
		debounce = true
		if not IsMoving then
			Door1.Door1["Close" .. math.random(1,3)]:Play()
			for i = 1, 47 do
				Door1.Door1.CFrame = Door1.Door1.CFrame + (Door1.Door1.CFrame.lookVector * 0.10)
				Door1.Door2.CFrame = Door1.Door2.CFrame + (Door1.Door2.CFrame.lookVector * 0.10)
			IsMoving = true
			local TeleportTo = script.Parent.Elevator2:FindFirstChild("Floor") -- Teleport part of the script
			if TeleportTo ~= nil then
				local Torso = Player.Character.Torso
				local Location = (TeleportTo.Position)
				local X = Location.X
				local Y = Location.Y
				local Z = Location.Z
				local LX = 0
				local LY = Y
				local LZ = 0
				Y = Y + math.random(2, 3)
				Torso.CFrame =,Y,Z),,LY,LZ))
				print("Fired 130")
			Door2.Door1["Open" .. math.random(1,3)]:Play()
			Floor = 2
			IsMoving = false
			for i = 1, 47 do
				Door2.Door1.CFrame = Door2.Door1.CFrame - (Door2.Door1.CFrame.lookVector * 0.10)
				Door2.Door2.CFrame = Door2.Door2.CFrame - (Door2.Door2.CFrame.lookVector * 0.10)
		debounce = false

Check if someone enters the elevator, add them to a table.
Check if they exit the elevator and remove them from the table
Teleport players who are in the table

this is just how i would do it idk if its good

1 Like

There is a problem with your script, if a players gets inside the elevator then decides to exit, he will still get teleported. You should force them to be inside and insert a GUI to let them choose if they want to exit or not. And if you want to know how many torsos are inside your elevator you should consider using a table that inserts the players once he’s inside and remove him once he gets teleported or exits.

the elevator involves a door system in which when a button is pressed the doors close. meaning if a player closed the door while they’re in the elevator thats their decision whether or not they wanted to teleport.

1 Like

Oh, sorry for the confusion.
if your script works like that then I don’t think it’s possible for one player to close the door for everyone, it should be done automatically.

1 Like

done automatically how? I was play testing with my friend and we were in the same elevator. I pressed the button but only I was teleported. This is with this script, the function only applies to the player who presses the button, and shown in the first line “Door1.Button2.ClickDetector.MouseClick:connect(function(Player)” the function is connected to the player who pressed the button and only him. I’ve thought of removing this player connection to the function, and instead, add a table where it counts the amount of torsos in an invisible part, which is located inside of the elevator. Problem is, is that I am unsure how to have the script count how many torsos are inside of this invisible part. I apologize for the confusion I must’ve gave you for the base question. It was quite rushed. I am very inexperienced with tables and so, have many questions for them.

  • This code does not check if a player has left the area!
    use table.remove(elevatorplayers, player) to remove someone from the table
local elevatorplayers = {}

local invispart = elevator.part -- just make it ur part

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then -- checking if it hit a player
table.insert(elevatorplayers, hit.Parent.HumanoidRootPart) -- inserting players humanoidrootpart into table 'elevatorplayers'

  for i,v in pairs(elevatorplayers:GetChildren() do -- for i,v loop to teleport every humanoidrootpart in the table
     v.CFrame = -- tp them where you want
    table.remove(elevatorplayers, v) -- removing them from the table once they've been teleported

I would recommend using ZonesPlus to check if someone is in an area, if you dont want to have a big “LEAVE” button when you enter the elevator because its difficult to use ‘.Touched’ to see if a player leaves


I get this error when its supposed to teleport

replace “local elevatorplayers = {}” with “elevatorplayers = {}”

similar issue

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