Weld part to player outside of character?

Why can’t I weld a part to the player which is not in the player character? This code works fine:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

local testPart = Instance.new("Part")
testPart.CanCollide = false
testPart.Parent = Character
 
local Weld = Instance.new("Weld", testPart)
Weld.Part0 = Character.HumanoidRootPart
Weld.Part1 = testPart
Weld.C1 = CFrame.new(2, -1, 0)

But this one doesn’t:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

local testPart = Instance.new("Part")
testPart.CanCollide = false
testPart.Parent = workspace
 
local Weld = Instance.new("Weld", testPart)
Weld.Part0 = Character.HumanoidRootPart
Weld.Part1 = testPart
Weld.C1 = CFrame.new(2, -1, 0)
1 Like

The second one does work with a small delay before parenting the part to workspace. Not entirely sure why though.

Parent the weld at the very last

That doesn’t work unless there’s a wait() before the parenting occurs. Why?

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

local testPart = Instance.new("Part")
testPart.CanCollide = false
testPart.Parent = workspace
 
local Weld = Instance.new("Weld")
Weld.Part0 = Character.HumanoidRootPart
Weld.Part1 = testPart
Weld.C1 = CFrame.new(2, -1, 0)
wait()
Weld.Parent = testPart

I think, its due to the HumanoidRootPart not existing in workspace when you spawn the part

I solved the issue by using

repeat wait()until Character:FindFirstChild("HumanoidRootPart")

after Character variable declaration.

Full script:
local Player = game:GetService(“Players”).LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

repeat
	wait()
until Character:FindFirstChild("HumanoidRootPart")

local testPart = Instance.new("Part", game.Workspace)
testPart.CanCollide = false
testPart.CFrame = Character.HumanoidRootPart.CFrame
 
local Weld = Instance.new("Weld", testPart)
Weld.Part0 = Character.HumanoidRootPart
Weld.Part1 = testPart
Weld.C1 = CFrame.new(2, -1, 0)
Weld.C0 = CFrame.new()
2 Likes