Weld all players from a table to a part

  1. What do you want to achieve? Hello.
    I have an elevator and i want to weld all players that are inside the elevator to the elevator floor.
    For that i have a script that check players that touch the lift floor and put it in a table.
    The script :
local PlayersInElevator = {}

LiftFloor.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player and not table.find(PlayersInElevator ,Player.Name) then
		table.insert(PlayersInElevator, Player.Name)
	end
end)

LiftFloor.TouchEnded:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player and table.find(PlayersInElevator ,Player.Name) then
		table.remove(PlayersInElevator, table.find(PlayersInElevator, Player.Name))
	end
end)

After that i weld the player to the floor of the lift with this script :

local Weld = Instance.new("WeldConstraint", LiftFloor)
				Weld.Part0 = LiftFloor
				Weld.Part1 = PlayersInElevator.Character.HumanoidRootPart
  1. What is the issue?
    When i do that i get this error : attempt to index nil with 'HumanoidRootPart’

  2. What solutions have you tried so far?
    I looked on forums and tried to fix it myself but it didn’t worked.

try using this:

for _, plr in pairs(PlayersInElevator) do
    local Weld = Instance.new("WeldConstraint", LiftFloor)
    Weld.Part0 = LiftFloor
    Weld.Part1 = plr.Character.HumanoidRootPart
end

Put this script just before the elevator goes a direction, as it will weld all users at that one point.

(The reason your script didn’t work is because you were trying to get the character of a table, not of the table value)

Hello, thank you for your answer, with the script you sent me i still get the same error

Can you please send your whole script (after my bit is added)

Can i send it to you in private message ? i don’t like to show my script in the public

If you wish to

randomtextrandomtextrandomtext

1 Like

The fix was:

for _, plr in pairs(PlayersInElevator) do
			local Weld = Instance.new("WeldConstraint", LiftFloor)
			Weld.Part0 = LiftFloor
			Weld.Part1 = game.Players[plr].Character.HumanoidRootPart
end
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).