Working on instancing a part into a player

I’ve been working on making a part that could be instanced into the player’s model in the workspace. The problem is in my output, it seems that it is always assigning nil to the HRP. I’ve tried looking at several videos and documentations but my script never worked.

game.Players.PlayerAdded:Connect(function(player)
	local part = Instance.new("Part")
	local chr = player
	local HRP = chr:FindFirstChild("HumanoidRootPart")
	print("yes")
	local newPart = part:Clone()
	newPart.Parent = chr
	newPart.Position = HRP.Position + Vector3.new(0,-3,0)
end)

The player doesnt have a root part. That would be the character.

Try this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(chr)
		local part = Instance.new("Part")
		local chr = player
		local HRP = chr:FindFirstChild("HumanoidRootPart")
		print("yes")
		local newPart = part:Clone()
		newPart.Parent = chr
		newPart.Position = HRP.Position + Vector3.new(0,-3,0)
	end)
end)

This way, Your character will always have the part when they spawn in.
Also, A little note about this, The player is an object inside the Players service, While characters are assigned to players, And spawn in the workspace.

2 Likes

It said in the output ServerScriptService.Script:9: attempt to index nil with ‘Position’.

Oh, This is because the character probably hasnt loaded their HRP, Use WaitForChild

local HRP = chr:WaitForChild("HumanoidRootPart")

Correct! If you need the character over a while you can use player.Character!!
It will return the character without need for the respawn!!!

I got an infinite yield, and the part isn’t in my model.

Did @InternalJohn41’s version work out??

Sadly no, waitforchild didnt work either.

I think I fixed it, it just spawned.

image

Oh, I just noticed, he redefined the chr variable for some reason. That’s probably why it didn’t work…

game.Players.PlayerAdded:Connect(function(player)
	local chr = workspace:WaitForChild(player.Name)
	local part = Instance.new("Part")
	local HRP = chr:FindFirstChild("HumanoidRootPart")
	print("yes")
	local newPart = part:Clone()
	newPart.Parent = chr
	newPart.Position = HRP.Position + Vector3.new(0,-3,0)
end)

This should work out!

The output said this, ServerScriptService.Script:9: Expected , got ‘end’.

I just need to like weld the part to the HRP.

Just edited! Now it should work out!! I would recommend using a weld constraint, if you want the part to be permanentaly attached to the HRP.

Btw, if you want to learn how to script I would recommend watching this tutorial: https://youtube.com/playlist?list=PLfie92NjUDbgOf5oqckr8XOyqg_q5B5WT&si=C2QRTbwhP8yCok0z

1 Like

Ok I’ll watch, I’ve been watching them for a while but I haven’t finished.