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

How do i make an NPC not attack a specific person?

Asked by
G0ZZEN 17
4 years ago
Edited 4 years ago

So I made an NPC that attacks other people how can I make that it doesn't attack the owner or someone specific? I currently have this as my chase script:

wait()
zombieParent = false

while zombieParent == false do

    wait()
    if script.Parent.Name == "Knight" then
        zombieParent = true
    end

end

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local waitTimer = 0

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 20
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    script.Parent.Target.Value = temp
                    dist = (temp.Position - pos).magnitude

                    if human.Health >= 1 then 
                    script.Parent.Range.Value = true 
                    else 
                    script.Parent.Range.Value = false 
                    end 
                end
            end
            if dist < 10 then 
                script.Parent.Attack.Value = true 
            else 
                script.Parent.Attack.Value = false 
            end 
        end
    end
    return torso
end

function Sit()
    if script.Parent.Zombie.Sit == true then 
        script.Parent.Zombie.Jump = true 
        print("Anti Seat Putter!!!")
    end 
end 

script.Parent.Zombie.Changed:connect(Sit)

function touch(hit)
if hit and hit.Parent then
if hit.Parent.Name == "Ballista" or  hit.Parent.Name == "Brotherhood Of Scythe" or hit.Parent.Name == "Jump" then
script.Parent.Zombie.Jump = true
end
end
end

script.Parent["Right Leg"].Touched:connect(touch)

script.Parent["Left Leg"].Touched:connect(touch)

while true do
    wait(1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        rx = math.random(-3,3)
        ry = math.random(0,0)
        rz = math.random(-3,3)
        script.Parent.Zombie:MoveTo(target.Position+Vector3.new(rx,ry,rz), target)
    else 
        script.Parent.Range.Value = false 
        script.Parent.Attack.Value = false 
    end
end

1 answer

Log in to vote
0
Answered by 4 years ago

This is a relatively simple solution, as where you can use conditional statements (also known as if statements) to determine things.

Here's a sample:

if 1 + 1 == 2 then
    print("Math")
else
    print("Math Broke")
end

So here, it will always print math since 1 + 1 is equivalent to 2. The concept is similar to imply into your script:

if Target.Name ~= "person" then
    -- do not attack
else
    -- attack
end

So where you get your target (assuming you know the script also assuming you made it), check the player or target via name or player user id if it is a player and then you will be done!

Hope this helped, best of luck!

Ad

Answer this question