Teleport Elevator only teleports the one first in the elevator

For some reason, whenever more than 1 player goes into the elevator, and the button is pressed, it only teleports the person who went in first. I am confused since I put a i, v in pairs so that the teleport applies to everyone in the elevatorplayers table. This issue is a bit complicated because there are multiple scenarios but all of them end in one similar fashion: The player who entered first will be teleported and only them. Everyone else in the elevator is stuck.

Scenario 1 - Player 1 pushes the elevator button so it goes down. However, Player 2 enters first. Player 1 enters and pushes the button inside the elevator so it can begin its teleport. However, Player 2 is the only person teleported.

Scenario 2 - Player 1 pushes the elevator button so it goes down. However, Player 1 enters first. Player 2 enters and Player 1 pushes the button inside the elevator so it can begin its teleport. However, Player 1 is the only person teleported.

The end results from these 2 Scenarios can be determined so that the player who enters first is the only one teleported. ALL PLAYERS SHOULD BE TELEPORTED.

This could have something to do with the first 2 .Touched functions, as they are quite unreliable. I attempted to use ZonePlus, however, when I write the code necessary for the Zone to be created, the entire script breaks.

elevatorplayers = {}

local invispart = script.Parent.InvisiblePart1 -- Invisible part inside elevator. This is floor 1.
invispart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then -- checking if it hit a player
		table.insert(elevatorplayers, hit.Parent.HumanoidRootPart)
		print(hit)-- inserting players humanoidrootpart into table 'elevatorplayers'
	end
end)

local invispart2 = script.Parent.InvisiblePart2 -- Invisible part inside elevator. This is floor 2.
invispart2.Touched:Connect(function(hit)
	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'
		print(hit)
	end
end)
Door1.Button1.ClickDetector.MouseClick:connect(function() -- Doors closing function
	if not debounce then
		debounce = true
		if not IsMoving and Floor == 2 then
			Door2.Door1["Close" .. math.random(1,3)]:Play()
			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)
				wait(.01)
			end
			IsMoving = true
			wait(7)
			BeepSound1:Play()
			Door1.Door1["Open" .. math.random(1,3)]:Play()
			Floor = 1
			IsMoving = false
			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)
				wait(.01)
			end
		elseif not IsMoving and Floor == 1 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)
				wait(.01)
			end
			IsMoving = true
			wait(7)
			BeepSound2:Play()
			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)
				wait(.01)
			end
		end
		debounce = false
	end
end)

Door1.ButtonScreen1.ClickDetector.MouseClick:connect(function() -- Doors closing function
	if not debounce then
		debounce = true
		if not IsMoving and Floor == 2 then
			Door2.Door1["Close" .. math.random(1,3)]:Play()
			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)
				wait(.01)
			end
			IsMoving = true
			wait(7)
			BeepSound1:Play()
			Door1.Door1["Open" .. math.random(1,3)]:Play()
			Floor = 1
			IsMoving = false
			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)
				wait(.01)
			end
		elseif not IsMoving and Floor == 1 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)
				wait(.01)
			end
			IsMoving = true
			wait(7)
			BeepSound2:Play()
			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)
				wait(.01)
			end
		end
		debounce = false
	end
end)

Door1.Button2.ClickDetector.MouseClick:connect(function() -- Teleport Function
	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)
				wait(.01)
			end
			MoveSound1:Play()
			IsMoving = true
			wait(7)
			local TeleportTo = script.Parent.Elevator2:FindFirstChild("Floor")
			if TeleportTo ~= nil then
				for i,v in pairs(elevatorplayers) do
					v.CFrame = CFrame.new(Vector3.new(-14.63, 34.661, 252.337))-- position of the floor to teleport to
						table.clear(elevatorplayers) -- Outside
					end
				end
			BeepSound2:Play()
			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)
				wait(.01)
			end
		end
		debounce = false
	end
end)

have you tried printing the value of elevator players before you decide to teleport the players and seeing what it outputs?

not before, but after seeing this I decided to give it a try.
image
Apparently, the script adds the same person multiple times. The number of this varies by how much the player moves around in the elevator. Perhaps making the .Touched function only apply once to a player can fix some issues. However, I don’t think it would solve the main issue right now. (this is from one player)

elevatorplayers = {}

local invispart = script.Parent.InvisiblePart1 -- Invisible part inside elevator. This is floor 1.

invispart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not elevatorplayers[player] then -- checking if it hit a player
		elevatorplayers[player] = hit.Parent.HumanoidRootPart
	end
end)

local invispart2 = script.Parent.InvisiblePart2 -- Invisible part inside elevator. This is floor 2.
invispart2.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not elevetorplayers[player] then -- checking if it hit a player
		elevatorplayers[player] = hit.Parent.HumanoidRootPart
	end
end)

The reason that the same person is added to the array multiple times is cuz you don’t check if they are already added beforehand

2 Likes

And the reason it only ups one player is cuz while looping you clear the table midway, stopping further iterations

Change this code:

for i,v in pairs(elevatorplayers) do
	v.CFrame = CFrame.new(Vector3.new(-14.63, 34.661, 252.337))-- position of the floor to teleport to
		table.clear(elevatorplayers) -- Outside
	end
end

To:

for i,v in pairs(elevatorplayers) do
	v.CFrame = CFrame.new(Vector3.new(-14.63, 34.661, 252.337))-- position of the floor to teleport to
end

for player, _ in elevatorplayers do
   elevatorplayers[player] = nil
end
2 Likes

oh wait nevermind! I apologize. Your scripts fixed the issue. Thank you so much!

didnt see that :man_facepalming:
\\\\\\\\\\

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