Holding an Object

Hello. So I’m currently trying to make a system where when a player walks up to an object and Presses E it picks up that Object. For testing reasons, I’m currently just using a block as the pickup item.

So the approach I was going to take is making it so that when they press E if there is an Object with a Value named Holdable nearby then change the name of the Value in their UI called CurrentlHolding to the name of the object. And that’s where I got stuck.

Now the first problem I’ve run into is having them pick up the closets one if there are more then 1 nearby. The second problem is having them put out their hands and have the object in their hands. I’m trying to avoid using tools. I’ve looked for tutorials and wiki’s on this but I’m having no luck so if anyone could point me in a direction to get started or some tutorials that’d be very helpful!

Kind Regards ZoomCode!

Problem 1

If there’s cases where there’s more than one items within your range, you can put the distances in a table, with references to the items, and sort it from lowest to highest magnitude with table.sort

Example usage of the sort function:

local t = {1, 56, 9, 124, 2840}

table.sort(t) -- without the "comp" argument provided, the function will sort from lowest to highest
print(unpack(t)) -- 1, 9, 56, ...

table.sort(t, function(a, b) -- with the "comp" argument provided, the function must return true if the first element given must come before the second
   return a > b
end)
print(unpack(t)) -- 2840, 124, 56, ...

Problem 2

For your second problem, you can try welding the object or attach it to player’s arm via a Motor6D. Motor6D’s as well as Welds work the same way: they have a Part0, which is the main part, and Part1, which is the part that is going to be attached to the Part0. They both have C0 and C1, which are relative offsets from Part0’s CFrame and C0 respectively, which position the Part1. The only difference between the two is that motor6ds make a movable joint that can manipulated by animations, while welds make a fixed joint

It might be kind of hard to position it, but here’s a function that can help you create either a Moror6d or Weld:

function attach(part0, part1, isMotor)
   local j = Instance.new(isMotor and "Motor6D" or "Weld")
   j.Part0 = part0
   j.Part1 = part1
   j.C0 = part0.CFrame:inverse() * part1.CFrame
   j.Name = part1.Name
   j.Parent = part0
end

Here’s articles about CFrames and Welds (The welds page seems to be down at the time of making this post) that can help you understand CFrames and welding

For having the player put out their hand, you can locate a stringvalue named ToolNoneAnim under the player’s Animate script and the animation itself should be parented to it. If not, you can search the script’s code to find the animation’s id, put it into an animation and parent it somewhere. You’ll have to load the animation using Humanoid:LoadAnimation and then play it using the :Play() function from the track returned from the aforementioned fucntion

3 Likes

Alright, I’ll give it a go Thank you!

1 Like