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

Zombie only targets playing when visible?

Asked by 8 years ago

This script works flawlessly. However, when any player is in range not in green team, it follows it and ignores every NPC closer to it. It almost ignores every enemy NPC and goes for the players. How can I fix this? I see nothing wrong with this and how it can be creating this bug.

local dist = math.huge

local function findNearestTorso(pos)
    local list = workspace:GetChildren()
    for x = 1, #list do
        local temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            local temp = temp2:findFirstChild("Torso")
            local human = temp2:findFirstChild("Humanoid")
            local player = game.Players:GetPlayerFromCharacter(temp2)
            if not player and (temp) and (human) and (human.Health > 0) and human.Parent.Name ~= "Zombie" then
                if (temp.Position - pos).magnitude < dist then
                    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
                        print("soldier")
                        torso = temp
                    end
                end
                elseif player and (temp) and (human) and (human.Health > 0) and player.TeamColor ~= BrickColor.new("Bright green") then
                if (temp.Position - pos).magnitude < dist then
                    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
                        print("player")
                    end
                end
            end
        end
    end
    return torso, tochr.magnitude
end

1 answer

Log in to vote
1
Answered by 8 years ago

Yes, because the second if statement makes sure the target is not a player

local dist = math.huge

local function findNearestTorso(pos)
    local list = workspace:GetChildren()
    for x = 1, #list do --cylces through each model
        local temp2 = list[x] --temp2 is set to each member of list
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            local temp = temp2:findFirstChild("Torso") 
            local human = temp2:findFirstChild("Humanoid")
            local player = game.Players:GetPlayerFromCharacter(temp2)  --player is set to it's corresponding character if they match, if not player returns false
            if not player and (temp) and (human) and (human.Health > 0) and human.Parent.Name ~= "Zombie" then
--this is where you are getting confused, it checks and makes sure the target is NOT a player
                if (temp.Position - pos).magnitude < dist then
                    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
                        print("soldier")
                        torso = temp
                    end
                end
                elseif player and (temp) and (human) and (human.Health > 0) and player.TeamColor ~= BrickColor.new("Bright green") then
                if (temp.Position - pos).magnitude < dist then
                    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
                        print("player")
                    end
                end
            end
        end
    end
    return torso, tochr.magnitude
end

Ad

Answer this question