I've been trying to make a beam ability and I used an attachment so the player can see where the beam ends and also where the raycast should end, however since the position of the attachment is decided by the Mouse.Hit.Position thing it goes on forever unless there is something in the way. So what I wanted to know is how to add a range limit onto that for example the attachment can't go further than 80 studs away from the humanoid root part. I've tried multiple things and none of them seem to work and the ones that do seem to set a limit on range cause the attachment to go to a completely random location.
local plr = game.Players.LocalPlayer local Mouse = plr:GetMouse() local maxDistance = 12 -- In studs Mouse.Button1Down:Connect(function() if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then print("Under max distance click!") end end)
Well, this little example shows how to use :DistanceFromCharacter
. It returns a number (studs) and it can be used for many things.
Article: Distance From Character
Edit: You can use magnitude as well: Magnitude