Make Part Follow Player When Button Is Clicked

Hi!
Im trying to make a part follow a player when a button in a gui is clicked.

I have been stuck on this code for the last day and i dont understand why it doesnt work.
The code worked when i wasnt using this within the clicking button function.


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 attachmentpet = Instance.new("Attachment")

				attachmentpet.Visible = false

				attachmentpet.Parent = pet

				local alignposition = Instance.new("AlignPosition")

				alignposition.MaxForce = 25000
				alignposition.Attachment0 = attachmentpet

				alignposition.Attachment1 = atachmentCharacter
				alignposition.Responsiveness = 25



				alignposition.Parent = newpet

				newpet.Parent = character
end
					
		end)

			clone.Equipped.MouseButton1Click:Connect(function(unequip)
				clicks = 0
				
				clone.Equipped.Visible = false
	
		end)
	end
end

Current error i am getting is:

Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.

Can anyone help? Thankyou!!

well you need to set the model’s primary part first to use :setPrimaryPartCFrame()

go into the model’s properties and select primary part and click a part inside the model

okay ive done that. Now it says “Attachments must be parented to a basepart”?

then parent the attachments to the part you want

Please do not use :SetPrimaryPartCFrame(), use :PivotTo() instead.

1 Like