Hello, I'm having a weird issue where my NPCs stop moving automatically even though there in a loop as seen here: https://gyazo.com/fee3e0c2f012e796487a128a5b007243
here is my movement script for the NPC:
local atkDistance = 4 local eTower = script.Parent:WaitForChild("eTower") local Tower = workspace.Game:FindFirstChild(eTower.Value) --/// myNPC local NPC = script.Parent.Parent local HRP = NPC:WaitForChild("HumanoidRootPart") local Hum = NPC:WaitForChild("Humanoid") local AI = NPC:WaitForChild("AI") local Team = AI:WaitForChild("Team") local Target = AI:WaitForChild("Target") local moveToTower = AI:WaitForChild("TouchTower") --/// Event local Event = script.Parent:WaitForChild("Trigger") local findTarget = script.Parent:WaitForChild("findTarget") Hum:MoveTo(Tower.Target.Position) Target.Changed:Connect(function() --/// Enemy NPC local eNPC = workspace.Game.npcStorage:FindFirstChild(Target.Value) local eHRP = eNPC:FindFirstChild("HumanoidRootPart") while true do wait(.1) if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then Target.Value = "" Hum:MoveTo(Tower.Target.Position) --/// Attack or Move elseif (HRP.Position - eHRP.Position).magnitude <atkDistance then -- Checks if the distance between the enemy and NPC is less than 5 studs Event:FireServer() elseif Target.Value ~= nil then Hum:MoveTo(eHRP.Position) --Hum.MoveToFinished:Wait() -- May improve srv performance but will make NPCs appear derpy as they may walk to far than suppose to end end end)
My NPC is programmed to chase down the nearest Enemy yet in studio it works fine-ish (Can still freeze but moves alot further than when im playing in real time.) If anyone has a solution please share it as i'd love to get this fixed as it will cause a big issue for my game if unsolved. Thanks
EDIT: I am also receiving an error from where it says "if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then" saying "attempt to index a nil value" This error only pops up once the NPCs have killed an enemy and ends up spamming my output bar. I also do not know how to counter this situation.
It is giving you the attempt to index a nil value
because once the the enemy is destroyed, the Humanoid is 'nil', and you are trying to access a property (health) of the Humanoid by doing:
eNPC:FindFirstChild("Humanoid").Health
You would want to test to see if the Humanoid even exists first before you check the health so doing something like:
if eNPC:FindFirstChild("Humanoid") then if Target.Value == "" or eNPC:FindFirstChild("Humanoid").Health <= 0 then -- stuff end end