local q=script.Parent local w=q.Parent local D=w.PlayerIcon local s=D:Clone()local e=w.Mode local B=w.Parent waitForChild(B,{"\073\100\101\110\116\105\102\105\101\114";"\076\111\099\097\116\105\111\110"})local g=B.Identifier local t=B.Location waitForChild(e,{"\083\101\097\114\099\104";"\084\097\114\103\101\116"})local M=e.Search local L=e.Target local Z={[BrickColor.new(226)]=true;[BrickColor.new(24)]=true,[BrickColor.new(106)]=true,[BrickColor.new(119)]=true,[BrickColor.new(1025)]=true,[BrickColor.new(38)]=true;[BrickColor.new(11)]=true;[BrickColor.new(1006)]=true;[BrickColor.new(1016)]=true;[BrickColor.new(21)]=true}local W={"\067\108\111\115\101\115\116","\070\097\114\116\104\101\115\116"}local function j()for q,w in next,W do if M.Value==w then return q end end end local function i(q)return Vector2.new(q.x,q.z)end local function K()for q,w in next,workspace:GetChildren()do if w:IsA("\077\111\100\101\108")and(findChildren(w,body)and not(w==character))then if not((i(w.Torso.Position)-i(character.Torso.Position)).magnitude>distance)then characters[#characters+1]={w,w.Torso.Position-character.Torso.Position}end end end end local function a(q,w)local D=#q:GetChildren()for w,s in next,w do for w=1,D,1 do if q:FindFirstChild(s)then q[s]:Destroy()end end D=#q:GetChildren()end end local function z(q)local w spawn(function()if not w then for q,w in next,Z do Z[q]=true end z(q)end end)for D,s in next,Z do if s then colors[q]=D w=true Z[D]=false return end end end local function I(q)if q=="\067\108\111\115\101\115\116"then return 50 elseif q=="\070\097\114\116\104\101\115\116"then return 0 end end local function F()local q=I(M.Value)local w=false for D,s in next,characters do local e=s[1]local B=s[2]local g=(i(B)).magnitude if M.Value=="\067\108\111\115\101\115\116"then if g<q then q=g L.Value=e.Name w=true end elseif M.Value=="\070\097\114\116\104\101\115\116"then if g>q then q=g L.Value=e.Name w=true end end end if not w then L.Value="\078\111\110\101"end end t.MouseButton1Click:connect(function()local q=j()local w=not(q+1>#W)and q+1 or 1 M.Value=W[w]end)M.Changed:connect(function()t.Text=M.Value.."\058"end);(game:GetService("\082\117\110\083\101\114\118\105\099\101")).RenderStepped:connect(function()a(w,{"\080\108\097\121\101\114"})local q=character:GetModelSize()local e=character.Head.CFrame.lookVector local B=camera.CoordinateFrame.lookVector local t=math.atan2(e.Z,e.X)D.Rotation=math.deg(t)+180 t=math.atan2(B.Z,B.X)w.Rotation=-(math.deg(t)-90)characters={}K()for e,B in next,characters do local g=s:Clone()local t=B[1]local M=B[2]local L=M.Y<-q.Y and"-"or M.Y>q.Y and"\043"or""local Z=t.Head.CFrame.lookVector local W=math.atan2(Z.Z,Z.X)local j,i=.4+(-M.X/distance)/3,.4+(-M.Z/distance)/3 if not colors[t]then z(t)end g.ZIndex=M.Y<-q.Y and D.ZIndex-1 or M.Y>q.Y and D.ZIndex+1 g.Rotation=math.deg(W)+180 g.TextColor=colors[t]or BrickColor.Random()g.Name="\080\108\097\121\101\114"g.Position=UDim2.new(j,0,i,0)g.Text=L.."\062"g.Parent=w end F()g.Text=L.Value end)


local scripts_folder = script.Parent;
local frame = scripts_folder.Parent;
local icon = frame.PlayerIcon;
local template = icon:Clone();
local mode_folder = frame.Mode;
local radar = frame.Parent;
waitForChild(radar, {'Identifier'; 'Location'})
local identifierLabel = radar.Identifier;
local locationButton = radar.Location;
waitForChild(mode_folder, {'Search'; 'Target'});
local mode = mode_folder.Search;
local target = mode_folder.Target;
local colorList = {
	[BrickColor.new(226)] = true;
	[BrickColor.new(24)] = true;
	[BrickColor.new(106)] = true;
	[BrickColor.new(119)] = true;
	[BrickColor.new(1025)] = true;
	[BrickColor.new(38)] = true;
	[BrickColor.new(11)] = true;
	[BrickColor.new(1006)] = true;
	[BrickColor.new(1016)] = true;
	[BrickColor.new(21)] = true;
}
local modeList = {
	'Closest';
	'Farthest';
}

local function getCurrentModeNumber()
	for index, choice in next, modeList do
		if mode.Value == choice then
			return index;
		end
	end
end

local function toV2(v3)
	return Vector2.new(v3.x, v3.z);
end

local function addCharactersInRange()
	for index, instance in next, workspace:GetChildren() do
		if instance:IsA('Model') and findChildren(instance, body) and not (instance == character) then
			if not ((toV2(instance.Torso.Position) - toV2(character.Torso.Position)).magnitude > distance) then
				characters[#characters + 1] = {instance; instance.Torso.Position - character.Torso.Position};
			end
		end
	end
end

local function clearParentOf(parent, list)
	local iterations = #parent:GetChildren();
	for index, name in next, list do
		for i = 1, iterations do
			if parent:FindFirstChild(name) then
				parent[name]:Destroy();
			end
		end
		iterations = #parent:GetChildren();
	end
end

local function assignColor(instance)
	local chosen
	spawn(function()
		if not chosen then
			for color, enabled in next, colorList do
				colorList[color] = true;
			end
			assignColor(instance);
		end
	end)
	for color, enabled in next, colorList do
		if enabled then
			colors[instance] = color;
			chosen = true;
			colorList[color] = false;
			return
		end
	end
end

local function returnRangeFromMode(mode)
	if mode == 'Closest' then
		return 50;
	elseif mode == 'Farthest' then
		return 0;
	end
end

local function searchPriority()
	local range = returnRangeFromMode(mode.Value);
	local result = false;
	for index, information in next, characters do
		local located = information[1];
		local deltaP = information[2];
		local current = toV2(deltaP).magnitude;
		if mode.Value == 'Closest' then
			if current < range then
				range = current;
				target.Value = located.Name;
				result = true;
			end
		elseif mode.Value == 'Farthest' then
			if current > range then
				range = current;
				target.Value = located.Name;
				result = true;
			end
		end
	end
	if not result then
		target.Value = 'None';
	end
end

locationButton.MouseButton1Click:connect(function()
	local current = getCurrentModeNumber();
	local nextNumber = not (current + 1 > #modeList) and current + 1 or 1;
	mode.Value = modeList[nextNumber];
end)

mode.Changed:connect(function()
	locationButton.Text = mode.Value .. ':';
end)

game:GetService('RunService').RenderStepped:connect(function()
	clearParentOf(frame, {'Player'});
	local characterSize = character:GetModelSize()
	local lookDirection = character.Head.CFrame.lookVector;
	local cameraDirection = camera.CoordinateFrame.lookVector;
	local rotation = math.atan2(lookDirection.Z, lookDirection.X);
	icon.Rotation = math.deg(rotation) + 180;
	rotation = math.atan2(cameraDirection.Z, cameraDirection.X);
	frame.Rotation = -(math.deg(rotation) - 90);
	characters = {};
	addCharactersInRange();
	for index, information in next, characters do
		local clone = template:Clone();
		local located = information[1];
		local deltaP = information[2];
		local symbol = deltaP.Y < -characterSize.Y and '-' or (deltaP.Y > characterSize.Y and '+' or '');
		local facingDirection = located.Head.CFrame.lookVector
		local rotation = math.atan2(facingDirection.Z, facingDirection.X);
		local xScale, yScale = 0.4 + (-deltaP.X / distance) / (3), 0.4 + (-deltaP.Z / distance) / (3);
		-- xScale = (not (xScale > 1) and xScale or 1) and (not (xScale < 0) and xScale or 0);
		-- yScale = (not (yScale > 1) and yScale or 1) and (not (yScale < 0) and yScale or 0);
		if not colors[located] then
			assignColor(located);
		end
		clone.ZIndex = deltaP.Y < -characterSize.Y and (icon.ZIndex - 1) or (deltaP.Y > characterSize.Y and (icon.ZIndex + 1));
		clone.Rotation = math.deg(rotation) + 180;
		clone.TextColor = colors[located] or BrickColor.Random();
		clone.Name = 'Player';
		clone.Position = UDim2.new(xScale, 0, yScale, 0);
		clone.Text = symbol .. '>'
		clone.Parent = frame
	end
	searchPriority();
	identifierLabel.Text = target.Value;
end)