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

:MoveTo() works when it's moving the model, but not the humanoid?

Asked by 7 years ago
Edited 7 years ago

In a script I'm working on, an NPC should move to the nearest player. However, in the current script I have, the NPC doesn't move at all. But, if I change line 42 to:

script.Parent:MoveTo(target.Position,target)

it directly teleports the NPC to the target player. But with the current line, the NPC doesn't gradually move towards the player. What am I doing wrong?

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 10000
    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("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(1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position,target) --this line
        print("moveto")
    end
end

Thanks!

0
You have to make the NPC look at the target, then make it move about 1 unit forward towards the target. I forgot how to do this since I don't do it but try something out! marioblast1244 113 — 7y
0
:MoveTo() is basically a teleport for model's like changing the position. nanelgamer 8 — 7y
0
if you want the npc to go to somewhere by walking use Humanoid.WalkToPart or Humanoid.WalkToPoint nanelgamer 8 — 7y
0
Your code works. I have tested it with a def rig. Did you anchor any parts of the npc? User#5423 17 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

:MoveTo() is basically a teleport for models like changing the position. if you want the NPC to go somewhere by walking use Humanoid.WalkToPart or Humanoid.WalkToPoint

0
Wrong frostysubatomiczero 51 — 7y
Ad

Answer this question