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:
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.
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
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
But you are right about to change EnemyPos to EnemyNear
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