How would I limit how far an item can be from the player?

BPCopy.CFrame = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)

I want to drag the object with the mouse, it works fine as is however I want to limit how far the item can be dragged from the player, I want the item to be moved within a certain radius around the player like 20 studs or so.

Edit: I also want to keep the original position on the Y axis so that the part stay on the ground instead of floating up in the air.

I think your best bet would be to calculate this before you set it’s CFrame.

So, some example code could be like…

local original_Y = BPCopy.CFrame.Y

function Update_CFrame()
local new_CF = CFrame.new(mouse.Hit.X, original_Y, mouse.Hit.Z)
if (BPCopy.CFrame.p - new_CF.p).Magnitude < 20 then
BPCopy.CFrame = new_CF
end
end

Something like this might work!

1 Like

This works although I used the HumanoidRootPart.CFrame.Position instead of the BPCopy.CFrame.Position. Thanks.

2 Likes