01 | while true do |
02 | v = game.Workspace.target |
03 | path = game:GetService( "PathfindingService" ):ComputeSmoothPathAsync(script.Parent.Torso.Position, game.Workspace.target.Position, 500 ) |
04 | points = path:GetPointCoordinates() |
05 | if path.Status.Name = = "Success" then |
06 | game.Workspace.Points:ClearAllChildren() |
07 | for p = 1 , #points do |
08 | part = Instance.new( "Part" ) |
09 | part.CanCollide = false |
10 | part.Size = Vector 3. new( 1 , 1 , 1 ) |
11 | part.Position = points [ p ] |
12 | part.Anchored = true |
13 | part.BrickColor = BrickColor.Black() |
14 | part.Parent = game.Workspace.Points |
15 | end |
i want to make a pathfinding NPC that goes to its target in workspace i tried to make this script above but this script moves the npc but does not make it follow the points. basically it doesnt follow the path the computer computes for it. please help me.
Here is something I was working on, it's not smooth but maybe you can learn from it.
Script in the Mob
01 | local AI = require(game.ServerScriptService.MobAI) |
02 | local Mob = game.Workspace.Mob |
03 | local Config = Mob.Configuration |
04 |
05 | while wait( 0.1 ) do |
06 | for i, v in pairs (game.Players:GetPlayers()) do |
07 | if (v.Character.Torso.Position - Mob.Torso.Position).magnitude > = 10 then |
08 | AI.CreatePath(Mob.Torso.Position, v.Character.Torso.Position, Mob, 0.2 , 200 ) |
09 | end |
10 | end |
11 | end |
ModuleScript in the ServerScriptService
01 | local MobAI = { } |
02 |
03 | local pathfinding = game:GetService( "PathfindingService" ) |
04 |
05 | function MobAI.CreatePath(start, finish, mob, speed, distance) --// Start Point : End Point : Mob : Speed of Mob : Mob Range |
06 | game.Workspace.Points:ClearAllChildren() |
07 | local path = pathfinding:ComputeSmoothPathAsync(start, finish, distance) -- Creates the path |
08 | local points = path:GetPointCoordinates() -- Gets the points |
09 |
10 | if path.Status = = Enum.PathStatus.Success then |
11 | for i = 1 , #points do -- Loops through the points |
12 | local part = Instance.new( "Part" , game.Workspace.Points) -- This whole section is creating visible bricks where the points are located |
13 | part.Size = Vector 3. new( 0.5 , 0.5 , 0.5 ) |
14 | part.Transparency = 0 |
15 | part.BrickColor = BrickColor.new( "Institutional white" ) |