Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why do my NPCs stop moving even though there on a while loop?

Asked by 5 years ago
Edited 5 years ago

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.

1 answer

Log in to vote
0
Answered by 5 years ago

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
0
You've got rid of some errors but now i've got one saying "attempt to index local 'eNPC' (a nil value)" NoirPhoenix 148 — 5y
0
this is on your 'line 1' NoirPhoenix 148 — 5y
0
When the NPC is killed I'm assuming it's destroyed in which cause you wouldn't even be able to find eNPC. So you need to do 2 things. One, make sure eNPC is not nil, and two make sure you redefine the eNPC to the current target. climethestair 1663 — 5y
Ad

Answer this question