Attach part to player

Hi i am trying to make a part follow a player when they click a button.
The part appears when the button is clicked but it doesnt follow the player. It just spawns next to them on the floor.

local MarketPlaceService = game:GetService("MarketplaceService")

local player = game:GetService("Players").LocalPlayer

local CatID = 87557759

local ItemFrame = script.Parent.Frame.ScrollingFrame

wait(player.Character:WaitForChild("HumanoidRootPart"))
local success, hasPass = pcall(function() -- Changing the pcall to the correct format
	return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, CatID)
end)

if success then -- Checking if successful
	if hasPass then -- Checking if the player has the


		local clone = script.Parent.Frame.ScrollingFrame.sample:Clone()

		clone.ImageLabel.Image = "http://www.roblox.com/asset/?id="..10974691853
		clone.Parent = script.Parent.Frame.ScrollingFrame

		clone.Visible = true
		clone.Equipped.Visible = false


		local equip = clone.Equip


		local clicks = 0

		local pet = game.ReplicatedStorage.Pet2

		equip.MouseButton1Click:Connect(function(equipped)

			clone.Equipped.Visible = true
			clicks += 1
			local newpet = pet:clone()

			local character = player.Character



			if character ~= nil then

				newpet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)


				local modelsize = newpet.PrimaryPart.Size

				local atachmentCharacter = Instance.new("Attachment")

				atachmentCharacter.Visible = false

				atachmentCharacter.Parent = character.HumanoidRootPart

				atachmentCharacter.Position = Vector3.new(1, 1, 0) + modelsize


				local alignposition = Instance.new("AlignPosition")

				alignposition.MaxForce = 25000

				alignposition.Attachment1 = atachmentCharacter
				alignposition.Responsiveness = 25



				alignposition.Parent = newpet

				newpet.Parent = character
			end

		end)

Heres the script i am currently using.

The script did say this -


				local attachmentpet = Instance.new("Attachment")

				attachmentpet.Visible = false

				attachmentpet.Parent = character

But it kept saying attachment must be parented to a basepart

Surely the attachment should be parented to the character to follow them?

Can anyone help to attach the part to the player?
Thankyou!

why did you create an entire new post just for a new error?

attachments MUST be parented to parts, that error is telling you need to parent the attachment you created to a part, not a model

well how do i attach it so it appears next to the player and follows them? Idk what part to attach it to??

what part do you want the part to connect to?

i want it so it follows behind the player in the air

Hello Tia!

Sorry for disturbing you with questions, but it would be really helpful if you could send a pic of the error, I can then try my best to assist you.

Hello.

Thats the error i am receiving!

partwithplayer

And this is how the part is appearing next to my player once i click equip.

I’d like it so the part follows my player in the air behind them.

Thankyou

Correct me if am wrong, but you need to have 2 attachments for AlignPosition, right? attachment 0 is the object which will be moved, attachment 1 will be the position to which it moves…

So try creating another attachment maybe

If you want, you can use lerp, it’s way easier for me to use.

what is lerp? does that allow parts to follow players?

yeah if you loop the lerp, you can make the part cframe into the humanoidrootpart cframe with a offset. It’s like tweenservice but better for this kind of use.

how would i incoorperate that into my code

alr so like this, dont just copy it btw

while task.wait() do
game.workspace.part.Cframe = game.workspace.Cframe:Lerp(plr.character.humanoidrootpart.Cframe * cframe.new(1.5,0,0),.015)
end

I feel like a better method than AlignPosition would be to use TweenService. You could cast a ray downward from their HumanoidRootPart to find the Y position of the pet, and you could find the X and Z by using the HumanoidRootPart’s CFrame as well. There are many great raycasting tutorials on YouTube if you need them.