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

How i can make that the NPC changes his speed when he is following you?

i have a little problem, How i can do that when the NPC are following you, its speed changes?

Here is the script

function findNearestTorso(pos)  --  declare pos a local vector3
    local list = game.Workspace:children()
    local torso = nil
    local dist = 25  -- Area to search
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if temp2.className == "Model" then
            temp = temp2:findFirstChild("Torso")
            if temp ~= nil then
                human = temp2:findFirstChild("Humanoid")
                if human ~= nil and (human.Health > 0) and (temp2.Name ~= AIName) then -- not named the same as us.
                    if (temp.Position - pos).magnitude < dist then
                        torso = temp
                        dist = (temp.Position - pos).magnitude
                    end  --  closer?
                end   --  human? Not us. 
            end -- Has torso?
        end   --  Model? 
    end  --  For. Loop thru parts in Workplace

    return torso
end  -- findNearest

2 comments

0

clearly you stole the script... and clearly if u wrote this, you would know the answer

0

^^

1 answer

just get the npc's humanoid

local NPC = script.Parent
local HumanoidOfNpc = Npc:FindFirstChild("Humanoid")
local speed = 20
function findNearestTorso(pos)  --  declare pos a local vector3
    local list = game.Workspace:children()
    local torso = nil
    local dist = 25  -- Area to search
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if temp2.className == "Model" then
            temp = temp2:findFirstChild("Torso")
            if temp ~= nil then
                human = temp2:findFirstChild("Humanoid")
                if human ~= nil and (human.Health > 0) and (temp2.Name ~= AIName) then -- not named the same as us.
                    if (temp.Position - pos).magnitude < dist then
                        torso = temp
                        dist = (temp.Position - pos).magnitude
			HumanoidOfNpc.WalkSpeed = speed
                    end  --  closer?
                end   --  human? Not us. 
            end -- Has torso?
        end   --  Model? 
    end  --  For. Loop thru parts in Workplace

    return torso
end  -- findNearest

This should help you define NPC and HumanoidOfNpc and speed for results

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