Cannot detect nearest enemy

Hello dear developers! I am pretty bad at scripting. I have this issue, I cannot make my NPC to find the nearest player and force my NPC to throw SuperBall at them. It’s an Enemy NPC that check the nearest player and throws SuperBall at them. I recently discovered this tutorial, and I am trying to follow steps on the tutorial but adding some tweaks to my script so it would be suitable for my NPC to work:

Projectile Physics - Roblox Scripting Tutorial

However, my code does not affect. When I switch the script exactly look like on the tutorial, it works! But when it comes to detecting the nearest player it does not do anything and output does not show any bugs or anything wrong with the script.

Here is my script:

local npc = workspace.Npc
local StartPos = Npc.HumanoidRootPart.Position

function EnemyNear()
	local nearestPlr = nil
	local shortestDistance = math.rad(7200)

	for _, plr in pairs(game.Players:GetPlayers()) do
		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
			local distance = (npc.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).magnitude

			if distance <= shortestDistance then
				nearestPlr = plr

				shortestDistance = distance
			end
		end
	end

	return nearestPlr
end


local EnemyPos = EnemyNear()

while EnemyPos do
	task.wait(3)
	local SuperBallclone = npc.ClassicSuperball.Handle:Clone()
	SuperBallclone.Position = StartPos + Vector3.new(0, 0, 1)
	SuperBallclone.Parent = workspace
end


Edit: I also have tried to move my spawn to near to my enemy NPC, and it did also not work.

Thank you for your time.

1 Like

change shortest distance and dont make it radians it should work

1 Like

wh at.

<= is very much the valid operator here. What im curious of is why you’re converting the shortest distance to radians, you dont need to do this.

7200 deg in radians is about 125

1 Like

radians is useless in this scenario like you stated

1 Like

It did not work, sadly, however! I found the solution while I was waiting for someone to respond. Thank you for your time, of course. So what is the solution, you may ask?

In the Script there is a for loop, the nearest enemy will be found only once, so if I respawn few seconds late or respawn far away from my enemy NPC, the nearest enemy will always be “nil” and it won’t repeat the script until it find the nearest enemy. It works only one time and stops.

So I decided to use repeat until

It will repeat the code, until the nearest enemy is not nil
repeat --code until nearestEnemy ~= nil
For studio to not crash, I added task.wait(1)

And surprisingly, it worked!
Here is the full code:

local npc = workspace.Npc
local StartPos = npc.HumanoidRootPart.Position


function EnemyNear()
	local nearestPlr = nil
	local shortestDistance = math.rad(7200)
	
	repeat
		task.wait(1)
		for _, plr in pairs(game.Players:GetPlayers()) do
			if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
				local distance = (npc.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).magnitude

				if distance >= shortestDistance then
					nearestPlr = plr

					shortestDistance = distance
				end
			end
		end
	until nearestPlr ~= nil
	
	print("Nearest Player: ", nearestPlr)
	return nearestPlr
end


local EnemyPos = EnemyNear()

while EnemyPos do
	task.wait(3)
	local SuperBallclone = npc.ClassicSuperball.Handle:Clone()
	SuperBallclone.Position = StartPos + Vector3.new(0, 0, 1)
	SuperBallclone.Parent = workspace
end


1 Like

actually use enemynear function in while loop and just replace while enemypos with while true. You dont need the repeat until

1 Like

There is a catch tho, while loop will loop without waiting for the nearest enemy to appear, so my NPC will throw SuperBalls even tho there is no nearest enemy :sweat_smile:
But you are right about to change EnemyPos to EnemyNear

1 Like

you check with a if statement to check player

Oh! Lemme try this out if it works or not.

1 Like

it does work use a if statement in the loop to check enemynear returns a player and remove the repeat until in the enemynear

1 Like

Not works, unfortunately. But thank you for your time and your help, not many people share their time to help. Cya soon!

1 Like

ill script it for you ill show you

1 Like

Sure! I would like to see your script, maybe I am doing something wrong.

1 Like

Try this code

local npc = workspace.Npc
local StartPos = npc.HumanoidRootPart.Position


function EnemyNear()
	local nearestPlr = nil
	local shortestDistance = 100
	
	
		
		for _, plr in pairs(game.Players:GetPlayers()) do
			if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
				local distance = (npc.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).magnitude

				if distance <= shortestDistance then
					nearestPlr = plr

					shortestDistance = distance
				end
			end
		end
	
	
	print("Nearest Player: ", nearestPlr)
	return nearestPlr
end




while true do
	task.wait(1)
     local EnemyPos = EnemyNear()
     if EnemyPos then

	    local SuperBallclone = npc.ClassicSuperball.Handle:Clone()
	    SuperBallclone.Position = StartPos + Vector3.new(0, 0, 1)
	     SuperBallclone.Parent = workspace
      end
end


2 Likes

This code will help you with it

2 Likes

This works GREAT! Thank you so much, man! :grin:

1 Like

cool it was probably how you scripted it

1 Like