Closest enemy system is shotty

I want to find the closest thug to the player. This script I made is like inconsistent and isn’t detecting thugs in the range. I have the range set to 45 studs, but it won’t detect any thugs outside of a range of like 30 studs.

Here’s a visual of what I mean:

Also, during some testing, I’m CLEARLY in front of the closest thug, but the script still says that the guy next to me is closer???

Here’s my code (Local Script in Starter Character Scripts):

local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local primaryPart = character:WaitForChild("HumanoidRootPart")

local closestThug = nil

local range = 45

local highLight = Instance.new("Highlight")

highLight.DepthMode = Enum.HighlightDepthMode.Occluded

highLight.OutlineTransparency = 0.05
highLight.FillTransparency = 0.95

highLight.OutlineColor = Color3.fromRGB(255, 255, 255)
highLight.FillColor = Color3.fromRGB(255, 255, 255)

RunService.RenderStepped:Connect(function()
	
	for _, thug in pairs(CollectionService:GetTagged("Thug")) do -- Loop through every thug in collection service

		if thug.Humanoid.Health > 0 then -- If the thug isn't dead
			 
			local distance = (primaryPart.Position - thug.PrimaryPart.Position).Magnitude -- Get the distance between the player and thug
			
			if distance <= range then -- If the distance is less than the range (45)

				if closestThug then -- If there is already a thug selected

					local newThugDistance = (primaryPart.Position - thug.PrimaryPart.Position).Magnitude -- Get the distance between the player and thug
					local oldThugDistance = (primaryPart.Position - closestThug.PrimaryPart.Position).Magnitude -- Get the distance between the player and already selected thug

					if newThugDistance < oldThugDistance then -- If the distance between the new thug and the player is lower less than the distance between the old thug and the player

						closestThug = thug -- Closest thug becomes the new thug
					else 
						
						continue
					end
				else

					closestThug = thug
				end
			else

				closestThug = nil
			end
		end
	end
	
	-- Just to highlight the closest thug:
	
	if closestThug then
		
		highLight.Parent = closestThug
		highLight.Adornee = closestThug
	else
		
		highLight.Parent = nil
		highLight.Adornee = nil
	end
	
	print(closestThug)
end)

Your distance check looks a bit complicated, so I’ll simplify it and it should work now.

Code:

local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local primaryPart = character:WaitForChild("HumanoidRootPart")

local highLight = Instance.new("Highlight")
highLight.DepthMode = Enum.HighlightDepthMode.Occluded
highLight.OutlineTransparency = 0.05
highLight.FillTransparency = 0.95
highLight.OutlineColor = Color3.fromRGB(255, 255, 255)
highLight.FillColor = Color3.fromRGB(255, 255, 255)

RunService.RenderStepped:Connect(function()
	local closestDistance = 45
	local closestThug = nil

	for _, thug in pairs(CollectionService:GetTagged("Thug")) do -- Loop through every thug in collection service
		if thug.Humanoid.Health > 0 then -- If the thug isn't dead
			local distance = (primaryPart.Position - thug.PrimaryPart.Position).Magnitude -- Get the distance between the player and thug

			if distance < closestDistance then
				closestDistance = distance
				closestThug = thug
			end
		end
	end
	
	-- Just to highlight the closest thug:
	highLight.Adornee = closestThug
	highLight.Parent = closestThug

	print(closestThug)
end)

For example:

Can be shortened into:

		highLight.Parent = closestThug
		highLight.Adornee = closestThug

Because if closestThug was nil, it would also set the Parent and Adornee to nil, which is intended.

Alright so this works, but how can I make it so if the thug is out of the 45 stud range, the closest thug is nil?

I tried this but then I got the same issue.

local distance = (primaryPart.Position - thug.PrimaryPart.Position).Magnitude
			
	if distance <= 45 then
		
		if distance < closestDistance then

			closestDistance = distance
			closestThug = thug
		end
	else
		
		closestThug = nil
	end

My script already does this, does it not?

Once a thug is detected inside the 45 studs range, it doesn’t go away once I get out of the 45 stud range.

Are you sure you’re using my exact code? This doesn’t happen when I test.

Whoops, I put the closestThug variable outside of the RunService loop because I needed it so I could pass it through a remote event. But once I put it inside the loop it was fine, like in your code. Sorry about that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.