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