Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

NPC wont stop following player?

So I've been working on an AI script. And it's working fine. But I cannot find out how to make the NPC turn around after following a player for some time and go back to its starting position. Heres my script:

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
local players = game:GetService("Players");

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 100
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2:IsA("Model")) and (temp2 ~= script.Parent) and (players:GetPlayerFromCharacter(temp2)) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            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
    return torso
end




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

end

2 comments

0

Have you tried using os.time() and adding a timer for when the NPC should stop following?

0

no. could you answer with an edit to the script?

1 answer

you could try npc:Destroy() ( ?° ?? ?°)

local npc = game:GetService("Workspace").npc
wait(69.5)
npc.HumanoidRootPart.CFrame = game.Workspace.NPCspawn

This discussion is preserved as it was. New answers, comments, and votes are closed.