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

My NPC goes Non-Aggro when it kills it's first victim?

Asked by 7 years ago

So I'm waiting for the npc to kill it's first victim... When it does, it calls the non-aggro function and stops even though there are other victims right next to it... I believe the bug is that it still detects the dead victim but still goes to the else statement because that victim's health is < 1. How am I supposed to fix this?? I'm so confused!

function findNearestTorso(pos)
    local list = game.Workspace:GetChildren()
    local torso = nil
    local dist = configs.Range.Value
    local temp = nil
    local human = nil
    local temp2 = nil
    for _,list in pairs(workspace:GetChildren()) do
        temp2 = list
        human = temp2:FindFirstChild("Humanoid")
        if (temp2.className == "Model") and human and (temp2 ~= script.Parent) and temp2:FindFirstChild("IsSoldier") and temp2:WaitForChild("IsSoldier").Value == false then
            temp = temp2:FindFirstChild("Torso")
            local player = game.Players:GetPlayerFromCharacter(temp2) 
            if not player and temp and human and temp.Transparency ~= 1 and human.Health > 0 and human.Parent.Name ~= "Soldier" and (temp.Position - pos).magnitude < dist then
                    local tochr = temp.Position - pos
                    local toPlayerRay = Ray.new(script.Parent.Head.Position, tochr)
                    local part = workspace:FindPartOnRay(toPlayerRay, script.Parent)
                    if part and part:IsDescendantOf(temp.Parent) then
                        torso = temp
                        Aggro()
                end
                elseif player and player.TeamColor ~= BrickColor.new("Bright blue") and temp and human and temp.Transparency ~= 1 and human.Health > 0 and (temp.Position - pos).magnitude < dist  then
                    local tochr = temp.Position - pos
                    local toPlayerRay = Ray.new(script.Parent.Head.Position, tochr)
                    local part = workspace:FindPartOnRay(toPlayerRay, script.Parent)
                    if part and part:IsDescendantOf(temp.Parent) then
                        torso = temp
                        Aggro()
                    end
            else
                print("no more targets wtf")
                torso = nil
                NonAggro()
            end
        end
    end
    return torso
end

Answer this question