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

Moving Bricks with Mouse but unable to set a range limit?

So today I coded a LocalScript which makes certain bricks with certain names movable when you are holding your left mouse button on them:

local plr = game.Players.LocalPlayer
repeat wait() until plr and plr.Character
local mouse = plr:GetMouse()
local mtarget
local down

mouse.Button1Down:connect(function()
 if mouse.Target ~= nil and mouse.Target.Locked == false and mouse.Target.Name == "BuildingBrick" or mouse.Target.Name == "BuildingShelf" then
  print(mouse.Target.Name)
  mtarget = mouse.Target
  down = true
  mouse.TargetFilter = mtarget or mouse.Target
  end
end)



mouse.Move:Connect(function()
 if down == true and mtarget ~= nil then
  mtarget.Position = Vector3.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)
 end
end)


mouse.Button1Up:connect(function()
 down = false
 mouse.TargetFilter = nil
 mtarget = nil
end)

All of this works, but I have been trying to set a range so the brick I'm moving doesn't move too far away from me, I tried a lot of things but I can't manage to work it out, help?

1 answer

✓ Accepted answer

You may find this of help. Try using this:

local distanceBetween = (plr.Character:WaitForChild("Head").Position - mouse.Target.Position).Magnitude < 10 -- change 10 the the distance you want (in studs)

You can use this with an "if" statement to restrict your part from going to far away.

It is called magnitude and it is a neat way to calculate the distance between you and your part. For more info, go to: https://developer.roblox.com/en-us/articles/Magnitude

2 comments

0

Thank you

0

You are most welome!

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