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

Issue with Getting NPC to follow. Error start at print(Humanoid)? Above this print works

Asked by
Hizar7 102
6 years ago
game.Players.PlayerAdded:connect(function(Player)
local p = game.Workspace:WaitForChild(Player.Name)
print (p)
local f = p.UpperTorso.Position
print (f)
local dist = 15
local pos1 = f
        for i,v in pairs(game.Workspace.CrystalMobA.UndeadWarrior:GetChildren())do
         if v.Name == "HumanoidRootPart" then
            print (v)
            local z = v.Parent.Humanoid
            local pos2 = v.Position
            local magnitude = (pos1 - pos2).magnitude
            if magnitude <=15 and z then
            if p.Humanoid ~= nil then 
                print('humanoid')
                while true do
    wait(1)
    local target = f(v.Position)
    if target ~= nil then
        print ("Following")
        v.Parent.Humanoid:MoveTo(target.Position, target)
                        end
                    end
                end
            end
        end
    end
end)

0
Line 2 is a bad way of getting the character, what if the characters name was "Part" OOF RIP greatneil80 2647 — 6y
0
Literally, everybody starting out to script gets their character this way. It's not wrong, but it's very inefficient. A Player object has a property called 'Charcater.' this property is your character, so to get the character, you just have to 'local p = Player.Character' oilsauce 196 — 6y
0
Also, you probably don't need the (v.Position) at line 19, local target = f(v.Position). I think it'll work if you just remove (v.Position). oilsauce 196 — 6y

Answer this question