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

Can someone help with how to make npcs, I've never done it before?

Asked by 2 years ago

how would I make a npc that moves and that can follow people around?

1 answer

Log in to vote
1
Answered by
Voruel 41
2 years ago

Some simple research or searching in the toolbox can solve your problem.

local PositionTorso = script.Parent.Torso.Position

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = math.huge
    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("UpperTorso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                end
            end
        end
    end
    return torso
end

while wait(0.15) do
    local target = findNearestTorso(PositionTorso)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position)
    end
end

This script does find the nearest torso and moves towards it every 0.15 seconds if it exists.

0
Put this inside the NPC model as a script Voruel 41 — 2y
Ad

Answer this question