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)