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

pathfinding script will not work right? [unanswered]

Asked by
MHaven1 159
8 years ago
Edited 8 years ago
01while 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 = Vector3.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
View all 30 lines...

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.

1 answer

Log in to vote
1
Answered by 8 years ago

Here is something I was working on, it's not smooth but maybe you can learn from it.

Script in the Mob

01local AI = require(game.ServerScriptService.MobAI)
02local Mob = game.Workspace.Mob
03local Config = Mob.Configuration
04 
05while 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
11end

ModuleScript in the ServerScriptService

01local MobAI = {}
02 
03local pathfinding = game:GetService("PathfindingService")
04 
05function 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 = Vector3.new(0.5,0.5,0.5)
14            part.Transparency = 0
15            part.BrickColor = BrickColor.new("Institutional white")
View all 30 lines...
0
Hopefully this helped!! xXLegendarySoldierXx 129 — 8y
0
ty MHaven1 159 — 8y
Ad

Answer this question