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

NPC won't follow a StringValue, why?

Asked by 4 years ago

This script is a basic code to make an NPC follow a player. Instead of the NPC trying to follow the player's torso, I want it to follow a String Value within the Torso called "Human". I tried several different ways to make it follow the StringValue but it won't. The issue is that it's looking for a physical part but a StringValue isn't a physical part within the Workspace. I have no idea how to solve this.

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 = 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("Torso") -- Here's the line that's giving me trouble 
            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(0.1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end

end

0
You mean a Humanoid? DeceptiveCaster 3761 — 4y
0
Yes, NPC or Humanoid. They're the same. Michael_TheCreator 166 — 4y

Answer this question