Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Adding a max distance to a bodyposition?

How would I add a max distance, so it does not fly away like so:

https://gyazo.com/7b2b95c47358acc93c00244fba3899a8

My current script:

local UIP = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

local M = Player:GetMouse()

  

  

function Send()

while wait() do

print(M.Hit.Position)

if M.Button1Down then

Position.Position = M.Hit.Position

end

end

end

  

  

M.Button1Down:Connect(function()

print(M.Hit.Position)

if M.Target ~= nil and M.Target:IsA("BasePart") then

if not(M.Target:FindFirstChild("BodyPosition")) then

Position = Instance.new("BodyPosition", M.Target)

Position.MaxForce = Vector3.new(1000000, 1000000, 1000000)

spawn(Send)

end

end

end)

1 comment

0

maybe Position.MaxForce = Vector3.new(1000000, 0, 1000000) ?? im not of any help when it comes to bodyposition/s

1 answer

You can store the original position of M.Target, then use a loop to check the distance until it's greater than some distance, then destroy the body position.

Store the original position before you create the BodyPosition to ensure that the position of the object before it moves is stored

local originalPos = M.Target.Position

After setting the position of the BodyPosition, add the loop as mentioned

repeat wait() until (M.Target.Position - originalPos).magnitude >= maxDistance

(M.Target.Position - originalPos).magnitude this gives the distance between the target's position and the original position. Don't forget to initialise maxDistance. After that, just destroy the BodyPosition as such

repeat wait() until (M.Target.Position - originalPos).magnitude >= maxDistance
Position:Destroy()

Hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.