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

How to limit the range mouse position?

I have this LocalScipt that displays text on your screen when your mouse hovers over a block. Like hover over a dropped item and it will say "Pick up". The only problem with is that the range is infinite, you can hover over an item from really far away and still see the message,

LocalScript inside StarterGui

script.Parent is a textlabel that follows your mouse

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	script.Parent.Position = UDim2.new(0, mouse.X + 10, 0, mouse.Y + 5)
	script.Parent.Visible = false
	local target = mouse.Target
	if target and target.Parent then
		if target.Parent:FindFirstChild("Part") then
			script.Parent.Text = "Click me!"
			script.Parent.Visible = true
		end
	end
end)

1 answer

✓ Accepted answer

Hello, 14dark14!

You can use (pos1 - pos2).magnitude to get the distance between 2 positions

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local range = 15.0 -- A variable to make it easyer to change

mouse.Move:Connect(function()
    script.Parent.Position = UDim2.new(0, mouse.X + 10, 0, mouse.Y + 5)
    script.Parent.Visible = false
    local target = mouse.Target
    if target and target.Parent then
	if (target.Position - player.Character.HumanoidRootPart.Position).magnitude < range then -- This will check for the distance from your character root part to the mouse target position
            if target.Parent:FindFirstChild("Part") then
                script.Parent.Text = "Click me!"
                script.Parent.Visible = true
            end
        end
    end
end)

4 comments

0

it works, but only backwards. If i walk close to the part i cant click it, but if I walk further away from it then I can click it

0

nvm I fixed it, the problem was that you added a greater not lower sign

1

ohhh yes, sorry

1

Can you mark the question as accepted as it solved th issue? I'll edit the script rn

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