local Players = game:GetService("Players")
local LiftFloor = script.Parent
local PlayersInElevator = {}
LiftFloor.Touched:Connect(function(Hit)
local HitModel = Hit:FindFirstAncestorOfClass("Model")
if HitModel then
local Player = Players:GetPlayerFromCharacter(HitModel)
if Player then
if not table.find(PlayersInElevator, Player) then
table.insert(PlayersInElevator, Player)
end
end
end
end)
LiftFloor.TouchEnded:Connect(function(Hit)
local HitModel = Hit:FindFirstAncestorOfClass("Model")
if HitModel then
local Player = Players:GetPlayerFromCharacter(HitModel)
if Player then
local TablePlayer = table.find(PlayersInElevator, Player)
if TablePlayer then
table.remove(PlayersInElevator, TablePlayer)
end
end
end
end)
local function WeldPlayersToElevator(Table)
for _, Player in ipairs(Table) do
local Character = Player.Character
if Character then
local HRP = Character:FindFirstChild("HumanoidRootPart")
if HRP then
local WeldConstraint = Instance.new("WeldConstraint")
WeldConstraint.Part0 = LiftFloor
WeldConstraint.Part1 = HRP
WeldConstraint.Parent = Character
end
end
end
end
WeldPlayersToElevator(PlayersInElevator)
You should be storing an array of player instances instead of an array of the string values assigned to the “Name” properties of player instances (their names), this allows for you to directly reference each player instance when iterating over the array, as opposed to needing to find the player instance again through the “PlayersService”. I’ve also added a minor improvement which allows for the “Handle” BasePart instance of accessories to trigger the “Touched” and “TouchEnded” events to fire. One final thing, avoid using the parent parameter of the “Instance.new()” constructor function. When setting the properties of an instance instanced by the “Instance.new()” constructor function it’s best to set the “Parent” property last (as I have done in the above).