Pickup Part Help

Hello devforum! I have yet another question. I’m trying to make a pickup part system, and i have the majority done. Here’s my code so far:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	
	local Clicker = game.Workspace:FindFirstChild(plr.Name)
	if Clicker then
		local TweenService = game:GetService("TweenService")

		local part = game.Workspace.Part
		part.Anchored = true
		local tweened = false

			if tweened == false then
			local goal = {}
			goal.Position = Clicker.Head.Position + Vector3.new(0, 2.5, 0)


			local tweenInfo = TweenInfo.new(0.9)

			local tween = TweenService:Create(part, tweenInfo, goal)

			tween:Play()
			wait(0.9)
			tweened = true
			end
		end
end)


This tweens the part above the character’s head, that works just fine. However, once the player starts moving, the part doesn’t follow. I’m just confused about how to go about making the part stay with the player until unanchored, effectively picking up and then dropping the object. I’ve tried welding it, but the part and the character met in the air and glitched together.

I appreciate any and all help! :slight_smile:

1 Like

You don’t have it wrapped in any sort of loop. What you’re doing is tweening the part above the player’s head once and that’s it. Use loops like .Stepped or .Heartbeat, or even while wait() do loops.

One thing to note is that tweening gets a little expensive in performance when done in a short amount of time multiple times, so I would suggest using BodyPosition or just simply setting the position directly.

The best way to do it in my opinion is to use BodyPosition, which updates every frame (using the .Stepped event above) to the position you want it to be in, in this case above the player’s head.

Some useful code from the Devhub:

local RunService = game:GetService("RunService")
 
local PART_START_POS = Vector3.new(0, 10, 0)
local PART_SPEED = Vector3.new(10, 0, 0)
 
-- Create a Part with a BodyPosition
local part = Instance.new("Part")
part.CFrame = CFrame.new(PART_START_POS)
local bp = Instance.new("BodyPosition")
bp.Parent = part
bp.Position = PART_START_POS
part.Parent = workspace
 
local function onStep(currentTime, deltaTime)
	-- Move the part the distance it is meant to move
	-- in the last `deltaTime` seconds
	bp.Position = bp.Position + PART_SPEED * deltaTime
	
	-- Here's the math behind this:
	-- speed = displacement / time
	-- displacement = speed * time
end
 
RunService.Stepped:Connect(onStep)

You also have some unnecessary stuff in your code, like the tweened bool or the wait after playing the tween, in case you want to do some housekeeping.

1 Like

It does not follow you as it isn’t attached to the player in any way. What I would do is tween the part, then weld the part to the character, and un anchor the part so it is able to be moved with the player. When you want to remove the part from them, simply delete the weld and the part will be free.

So the problem behind my weld attempt was because it was anchored? That makes sense, but how would I “release” the weld to “drop” the part?

1 Like

To make the part stay on top of your character’s head, you will need to use AlignPosition which will make sure that the part aligns with the position at all times.


local function AlignPartOnTopOfHead(Head, Part)

local HeadPosAttachment = Instance.new("Attachment", Head)
local PartPosAttachment = Instance.new("Attachment", Part)

HeadPosAttachment.Position = Vector3.new(0, 10, 0) -- position offset

local AlignPosition = Instance.new("AlignPosition", Part)
AlignPosition.Attachment0 = PartPosAttachment
AlignPosition.Attachment1 = HeadPosAttachment
AlignPosition.RigidityEnabled = true -- remember to set this to true so that the force acts on the attachments automatically

end

2 Likes

Yeah, if you ever insert a part that’s attached to the player and it’s anchored, the player would be unable to move I’m pretty sure the weld will release if you either destroy it or change the Part0 and Part1.

1 Like

Thank you! I didnt think to unanchor it :slight_smile: It’s fixed now!

1 Like