How can I make multiple parts weld to players

So I have 4 parts on my workspace and I want every single part to weld to a player. What I mean is. Let’s say there is part 1,2,3,4 on the map. When player 1 joins, then part 1 welds instantly to player 1. Then player 2 joins, and part 2 welds instantly to player 2.

I don’t know how to make that because I don’t know how I can reference other players and I can’t think of any idea that would help.

I tried finding something similar to devforum but couldn’t find anything.

local tracking = workspace.Tracking --folder with parts
local part1 = tracking.Part1
local part2 = tracking.Part2
local part3 = tracking.Part3
local part4 = tracking.Part4

local weld = Instance.new("WeldConstraint")
weld.Parent = character
weld.Part0 = part1
weld.Part1 = character.HumanoidRootPart
part1.Position = character.HumanoidRootPart.Position
2 Likes

What you can do is put the parts you want to weld inside of a table or use GetChildren though not sure how it will affect ordering.

Detect when a player joins via Players.PlayerAdded, put the return of table.remove on the first index of your table into a variable, that’ll be the part to weld, then weld it to their character

Some pseudo code

local parts = workspace.Tracking:GetChildren()

Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local part = table.remove(parts, 1)
    -- Weld here
end)
2 Likes

Can you go into more detail on this part of the script please?

local part = table.remove(parts, 1)
2 Likes

table.remove removes the specified index, in this case, it’ll remove the first thing in the table. The function also returns you the removed item.

So that line removes the first index from the table but puts it in the part variable

local tbl = {"Bob", "Steve"}

local name = table.remove(tbl, 1)

print(tbl) -- {"Steve"}
print(name) -- "Bob"
1 Like

When I tested it with one player, it worked properly but I got this error :
PlayerAdded is not a valid member of Player “Players.Raven_fortnite3”

I’ll try with many players to see if it worked

1 Like

I think you’re confusing how PlayerAdded works, it’s an event of the Players Service, not of individual players, what is the code you tried to use?

1 Like

Oh sorry my bad. I thought Player was like, local player. Ill fix it. Also it worked with 2 players.

1 Like

That should mean it should work with 3 and 4 players as well, give it a try if you want.

Though make sure the parts in your folder and the amount of players that can be in a server match otherwise you’ll get some errors because it can’t find anymore parts

3 Likes

Hey, I tested it again and it doesn’t work. I went testing with 2 players and the second player doesn’t have a part welded into him. What should I do?

1 Like

Only the first player has a part welded into him.

1 Like

Can you show me the code you have so far? Probably something wrong there

1 Like
local parts = {
	part1,
	part2,
	part3,
	part4
}

Player.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local part = table.remove(parts, 1)
	local weld = Instance.new("WeldConstraint")
	weld.Parent = character
	weld.Part0 = part
	weld.Part1 = character.HumanoidRootPart
	part.Position = character.HumanoidRootPart.Position
end)

I changed to table and I didn’t put other variables in here.

2 Likes

Hmm, maybe the order is execution is a bit out of place? Does it put a weld in the 2nd character at least? If so, then maybe you might need to change the execution, try positioning, then setting part0 and part1 and then setting the parent?

3 Likes
Player.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local part = table.remove(parts, 1)
	local weld = Instance.new("WeldConstraint")
	part.Position = character.HumanoidRootPart.Position
	weld.Part0 = part
	weld.Part1 = character.HumanoidRootPart
	weld.Parent = character
end)

something like this?

2 Likes

Should work, though again I’m not sure if it puts a weld in the 2nd player or if any errors are involved, see if changing the order works

3 Likes

Doesn’t work, I tested it with 2 people and nothing changed

2 Likes

Any errors?

Does it show the weld in the character that it doesn’t work for? (If you’re doing a local server test)

2 Likes

Now no player has a part, not even the first one. And no errors

2 Likes

Could it be something up with the parts themselves? I tested it out myself and they welded just fine for me using a default part you get when you create a part. Which is even more odd. Could maybe also be where the script was placed? Where did you put it

What are their properties? I’m not sure what exactly could be up

1 Like

Wait actually, can you save as the place and upload it here so I can put it into my place?

2 Likes