Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
2ndwann 131
4 years ago
Edited 4 years ago

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

0
Thank you MasterChanneLXL 7 — 4y
0
You are most welome! 2ndwann 131 — 4y
Ad

Answer this question