Ad
Log in to vote
0

How would I go about making an NPC hostile?

Asked by 10 days ago

So I am currently trying to make an enemy NPC that when you get close to it, it chases you and attacks you. Then after chasing you for a set distance, it will return back to its original position. However, I don't know where to start. I am aware that I am not providing a script to be fixed, but I would highly appreciate it if someone could point me in the right direction. Please help and thank you.

0
I think it's an option, unsure, have you checked the dev wikia? Twenty1chickens 2 — 10d

1 answer

Log in to vote
0
Answered by 10 days ago

I don't know what your variables are but feel free to use this. This is a follow & damageable script

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

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    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
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    if(dist <= 30) then
        if(dist <= 3) then
            human:TakeDamage(15)
        end
    return torso
    else 
    return script.Parent.Torso
    end
end




while true do
    wait(1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end

end

Here is a npc health script if needed (Grabbed off of wiki)

function waitForChild(parent, childName)
        local child = parent:findFirstChild(childName)
        if child then return child end
        while true do
        child = parent.ChildAdded:wait()
        if child.Name==childName then return child end
        end
        end

        -- declarations

        local Figure = script.Parent
        local Head = waitForChild(Figure, "Head")
        local Humanoid = waitForChild(Figure, "Humanoid")

        -- regeneration
        while true do
        local s = wait(1)
        local health = Humanoid.Health
        if health > 0 and health < Humanoid.MaxHealth then
        health = health + 0.01 * s * Humanoid.MaxHealth
        if health * 1.05 < Humanoid.MaxHealth then
        Humanoid.Health = health
        else
        Humanoid.Health = Humanoid.MaxHealth
        end
        end
        end

0
Thanks! But if you can, could you also please explain to me what each part in the follow/damage script do? SweetNoodleSoup 12 — 9d
0
SweetNoodleSoup, The Follow script is a simple noticePlayer script. Damage script declares the amount of damage it will do to the player on contact Pixelifix 10 — 9d
Ad

Answer this question