Enemies targeting the last person joined

Chello fellow robloxian!
I encountered an issue with my script.
Expected output : Enemies will follow player closest to them
Actuall output : Enemies follow the latest person that joined the game first and ignore even closest player(s).
Script:

local enemy = script.Parent
local eHumanoid = enemy:WaitForChild("Humanoid")
local eTorso = enemy:WaitForChild("Torso")

local debounce = false

local damage = enemy:WaitForChild("Damage")
local delai = enemy:WaitForChild("AttackDelay")

enemy.HumanoidRootPart:SetNetworkOwner(nil)
eHumanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

local players = game:GetService("Players")

local tweenService = game:GetService("TweenService")

local distance = 300

local function findNearestTarget()
	local temporary = nil
	
	for _, I in pairs(game.Workspace:GetChildren()) do
		if I:IsA("Model") and I ~= enemy then
			local key = I:FindFirstChild("IsAnEnemy")
			if key and key.Value == false then
				local torso = key.Parent:FindFirstChild("Torso")
				if torso then
					local dist = (torso.Position - eTorso.Position).Magnitude
					if dist < distance then
						temporary = torso
					end
				end
			end
		end
	end
	
	return temporary
end

enemy["Right Arm"].Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("IsAnEnemy") and debounce == false then
		local hum = otherPart.Parent:FindFirstChild("Humanoid")
		if hum and otherPart.Parent:FindFirstChild("IsAnEnemy").Value == false and eHumanoid.Health > 0 then
			debounce = true
			hum:TakeDamage(damage.Value)
			task.wait(delai.Value)
			debounce = false
		end
	end
end)

enemy["Left Arm"].Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("IsAnEnemy") and debounce == false then
		local hum = otherPart.Parent:FindFirstChild("Humanoid")
		if hum and otherPart.Parent:FindFirstChild("IsAnEnemy").Value == false and eHumanoid.Health > 0 then
			debounce = true
			hum:TakeDamage(damage.Value)
			task.wait(delai.Value)
			debounce = false
		end
	end
end)

local newInfo = TweenInfo.new(3)

local function begone(target)
	for _, I in pairs(target:GetChildren()) do
		if I:IsA("Part") or I:IsA("MeshPart") then
			local tween = tweenService:Create(I, newInfo, {Transparency = 1})
			tween:Play()
		end
	end
	
	task.wait(3)
	target:Destroy()
end

eHumanoid.HealthChanged:Connect(function(newHealth)
	if newHealth > 0 then return end
	if eHumanoid:GetStateEnabled(Enum.HumanoidStateType.Dead) then return end
	eHumanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, true)
	eHumanoid:ChangeState(Enum.HumanoidStateType.Dead)
end)

eHumanoid.Died:Connect(function()
	begone(enemy)
end)

while true do
	local g = findNearestTarget()
	
	if g then
		eHumanoid:MoveTo(g.Position)
	end
	task.wait(0)
end

Helps will be appreciated. Thanks :smile:

In order to make the enemies target the closest person you need to make a variable which stores the temporary closest distance to the players and then check that the dist is smaller than the last closest distance.
Here’s the new code

local enemy = script.Parent
local eHumanoid = enemy:WaitForChild("Humanoid")
local eTorso = enemy:WaitForChild("Torso")

local debounce = false

local damage = enemy:WaitForChild("Damage")
local delai = enemy:WaitForChild("AttackDelay")

enemy.HumanoidRootPart:SetNetworkOwner(nil)
eHumanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

local players = game:GetService("Players")

local tweenService = game:GetService("TweenService")

local distance = 300

local function findNearestTarget()
	local temporary = nil
	local temporaryDistance = math.huge
	
	for _, I in pairs(game.Workspace:GetChildren()) do
		if I:IsA("Model") and I ~= enemy then
			local key = I:FindFirstChild("IsAnEnemy")
			if key and key.Value == false then
				local torso = key.Parent:FindFirstChild("Torso")
				if torso then
					local dist = (torso.Position - eTorso.Position).Magnitude
					if dist < distance and dist < temporaryDistance then
						temporary = torso
						temporaryDistance = dist
					end
				end
			end
		end
	end
	
	return temporary
end

enemy["Right Arm"].Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("IsAnEnemy") and debounce == false then
		local hum = otherPart.Parent:FindFirstChild("Humanoid")
		if hum and otherPart.Parent:FindFirstChild("IsAnEnemy").Value == false and eHumanoid.Health > 0 then
			debounce = true
			hum:TakeDamage(damage.Value)
			task.wait(delai.Value)
			debounce = false
		end
	end
end)

enemy["Left Arm"].Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("IsAnEnemy") and debounce == false then
		local hum = otherPart.Parent:FindFirstChild("Humanoid")
		if hum and otherPart.Parent:FindFirstChild("IsAnEnemy").Value == false and eHumanoid.Health > 0 then
			debounce = true
			hum:TakeDamage(damage.Value)
			task.wait(delai.Value)
			debounce = false
		end
	end
end)

local newInfo = TweenInfo.new(3)

local function begone(target)
	for _, I in pairs(target:GetChildren()) do
		if I:IsA("Part") or I:IsA("MeshPart") then
			local tween = tweenService:Create(I, newInfo, {Transparency = 1})
			tween:Play()
		end
	end
	
	task.wait(3)
	target:Destroy()
end

eHumanoid.HealthChanged:Connect(function(newHealth)
	if newHealth > 0 then return end
	if eHumanoid:GetStateEnabled(Enum.HumanoidStateType.Dead) then return end
	eHumanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, true)
	eHumanoid:ChangeState(Enum.HumanoidStateType.Dead)
end)

eHumanoid.Died:Connect(function()
	begone(enemy)
end)

while true do
	local g = findNearestTarget()
	
	if g then
		eHumanoid:MoveTo(g.Position)
	end
	task.wait(0)
end
1 Like

you need to memorize the minimum distance each time

local minDistance = 300
local dist = (torso.Position - eTorso.Position).Magnitude
if dist < minDistance then
	minDistance = dist
	nearestTarget = playerModel
end
1 Like

Thanks for both of you! Thanks for helping me solve this issue!

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