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

How can I make an NPC walk infinitely from point A to B (and then B to A)?

Asked by 4 years ago
Edited 4 years ago

Was wondering how I can make an NPC (dummy) walk from one point to another, and then back an infinite amount of times. Thanks!

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

MoveTo:

:MoveTo, is a primary function of the Humanoid, it's purpose is to move the player.

local Player_Name = "Hillary"

game.Workspace[Player_Name].Humanoid:MoveTo(workspace.Part)

Parameters;

1st parameter; Position, this concludes where we want to move the player to, so in the code above me, we moved the player to the part.

Code;

This would move the player infinitely between the two points you mentioned;

local NPCName = "Max" -- the name of your npc
local Point_A_Name = "PointA" -- the name of the first place you want to move him to
local Point_B_Name = "PointB"

function Move(place)
    game.Workspace[NPCName].Humanoid:MoveTo(place)
    game.Workspace[NPCName].Humanoid.MoveToFinished:Wait()
end

while wait() do
    Move(game.Workspace[Point_B_Name].Position)
    Move(game.Workspace[Point_A_Name].Position)
end

Aha! There you go.

:MoveToFinished, waits until the player has moved to the specific point.

Improving your code, with pathfinding.

Let's say the path between the two points are very hard to get to, well in this case the NPC has no brain so it would walk straight into the wall if there was a hard path.

So we use PathFinding!;

local NPCName = "Max"
local Point_A_Name = "PointA"
local Point_B_Name = "PointB"

local PathFindingService = game:GetService("PathfindingService")

workspace[NPCName].HumanoidRootPart:SetNetworkOwner(nil)


function PathFind(place)
    local Path = PathFindingService:CreatePath()
    Path:ComputeAsync(workspace[NPCName].Torso.Position, place)
    local WayPoints = Path:GetWaypoints()

    for i, waypoint in pairs(WayPoints) do
        if waypoint.Action == Enum.PathWaypointAction.Jump then
            workspace[NPCName].Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        end

        workspace[NPCName].Humanoid:MoveTo(waypoint.Position)
        workspace[NPCName].Humanoid.MoveToFinished:Wait()   
    end
end

while wait() do
    PathFind(game.Workspace[Point_B_Name].Position)
    PathFind(game.Workspace[Point_A_Name].Position)
end

maxpax2009 Here; Keep Scripting!

Ad
Log in to vote
-1
Answered by 4 years ago

you can use a mixture of a For loop and Pathfinding Service

Answer this question