How do I limit the mouse range?

I am using an opensource RankGui script (when you place your mouse on someone it shows their name and ranks). I want to limit the range so you cannot see the players name from half the map away.

I essentially need to limit the range of the mouse.

Photo

image

Here is the Script
local Main = 10165178      -- Group ID (url number)

-----

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local CurrentTarg

local Frame = script.Parent.Frame
local Username = Frame.Username
local Template = script.Template
                                           
local Allies = {GroupService:GetGroupInfoAsync(Main)}

local Cache = {}

local Pages = GroupService:GetAlliesAsync(Main)

while true do
	for i, group in pairs(Pages:GetCurrentPage()) do
		table.insert(Allies, group)
	end
	if Pages.IsFinished then
		break
	end
	Pages:AdvanceToNextPageAsync()
end

local function return_matching(plr) 
	local matching_groups = {}
	for _, ally in ipairs(Allies) do
		if plr:IsInGroup(ally.Id) then
			table.insert(matching_groups, ally.Name .. " - " .. plr:GetRoleInGroup(ally.Id))
		end
	end
	return(matching_groups)
end

local function setup_cache()
	local players = Players:GetPlayers()
	for i = 1, #players do
		local plr = players[i]
		Cache[plr.Name] = return_matching(plr)
	end
end

Players.PlayerAdded:Connect(function(plr)
	Cache[plr.Name] = return_matching(plr)
end)

setup_cache()

RunService:BindToRenderStep("Mouse", 1, function()  -- gui
	local pos = UserInputService:GetMouseLocation()
	Frame.Position = UDim2.new(0, pos.X, .0125, pos.Y)
	local Target = Mouse.Target
	if Target then
		local PlayerTarg = Players:GetPlayerFromCharacter(Target:FindFirstAncestorOfClass("Model")) or nil
		if PlayerTarg then
			if PlayerTarg ~= CurrentTarg and PlayerTarg ~= Player then
				CurrentTarg = PlayerTarg
				Frame.Username.Text = PlayerTarg.Name
				local target_cache = Cache[PlayerTarg.Name]
				local FrameX, FrameY = Frame.Size.X, Frame.Size.Y
				Frame.Size = UDim2.new(FrameX.Scale, FrameX.Offset, FrameY.Scale, 20 + (#target_cache * Template.Size.Y.Offset))
				for i, v in ipairs(target_cache) do
					local Label = Template:Clone()
					Label.Text = v
					Label.Parent = Frame
				end
			end
		else
			CurrentTarg = nil
			Frame.Username.Text = ""
			local frame_children = Frame:GetChildren()
			if #frame_children > 2 then
				for i, v in ipairs(frame_children) do
					if v:IsA("TextLabel") and v.Name ~= "Username" then 
						v:Destroy()
					end
				end
			end
		end
	end
end)

All and any help is appreciated.

You could use a magnitude from the Mouse.Target and the local player’s character…

Like @Geolio9 said, you can calculate the distance between the players by doing some simple vector math and reading the magnitude.

For example:

(char1.Head.Position - char2.Head.Position).Magnitude <= 15

This would return true if the player is within 15 studs of the other character and false if not.

5 Likes