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

How do I add a range limit to mouse.hit?

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.

2 comments

0

Use player:DistanceFromCharacter(Mouse.Hit.Position)

0

How would I go about using that?

1 answer

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

2 comments

0

I see what you mean but what I want to achieve is for the beam to still go even if for example the mouse is pointing at the sky but it stops at a certain distance rather than going on forever

0

Figure out, you might need to use Mouse.Target to detect if an instance is in the way

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