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
while true do
    v = game.Workspace.target
    path = game:GetService("PathfindingService"):ComputeSmoothPathAsync(script.Parent.Torso.Position, game.Workspace.target.Position, 500)
    points= path:GetPointCoordinates()
    if path.Status.Name == "Success" then
        game.Workspace.Points:ClearAllChildren()
        for p = 1, #points do
            part = Instance.new("Part")
            part.CanCollide = false
            part.Size = Vector3.new(1,1,1)
            part.Position = points[p]
            part.Anchored = true
            part.BrickColor = BrickColor.Black()
            part.Parent = game.Workspace.Points
            end
            for i=1,10 do
                local part,point=workspace:FindPartOnRay(Ray.new(points[math.min(#points,i)],Vector3.new(0,-10,0)))
                local part,point2=workspace:FindPartOnRay(Ray.new(points[math.min(#points,i+1)],Vector3.new(0,-10,0)))
                local count=0
                while math.min((point2-v.Position).magnitude,(point*Vector3.new(1,0,1)-v.Position*Vector3.new(1,0,1)).magnitude)>3 and count<50 do
                    count=count+1
                    script.Parent:FindFirstChild("Humanoid"):MoveTo(point2)
                    if workspace:FindPartOnRay(Ray.new(v.Position,CFrame.new(v.Position*Vector3.new(1,0,1),point2*Vector3.new(1,0,1)).lookVector*4))then
                        script.Parent:FindFirstChild("Humanoid"):MoveTo(point)
                end
            end
        end
    end
    wait(0.1)
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.

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

local AI = require(game.ServerScriptService.MobAI)
local Mob = game.Workspace.Mob
local Config = Mob.Configuration

while wait(0.1) do
    for i, v in pairs (game.Players:GetPlayers()) do
        if (v.Character.Torso.Position - Mob.Torso.Position).magnitude >= 10 then
            AI.CreatePath(Mob.Torso.Position, v.Character.Torso.Position, Mob, 0.2, 200)
        end
    end
end

ModuleScript in the ServerScriptService

local MobAI = {}

local pathfinding = game:GetService("PathfindingService")

function MobAI.CreatePath(start, finish, mob, speed, distance) --// Start Point : End Point : Mob : Speed of Mob : Mob Range
    game.Workspace.Points:ClearAllChildren()
    local path = pathfinding:ComputeSmoothPathAsync(start, finish, distance) -- Creates the path
    local points = path:GetPointCoordinates() -- Gets the points

    if path.Status == Enum.PathStatus.Success then
        for i = 1, #points do -- Loops through the points
            local part = Instance.new("Part", game.Workspace.Points)  -- This whole section is creating visible bricks where the points are located
            part.Size = Vector3.new(0.5,0.5,0.5)
            part.Transparency = 0
            part.BrickColor = BrickColor.new("Institutional white")
            part.CanCollide = false
            part.Anchored = true
            part.Position = points[i]
        end
--[[        local anim = mob.Enemy:LoadAnimation(mob.Enemy.WalkAnim)
        anim:Play()     --]] -- Not neccessary, for more aesthetic reasons.
        for i = 1, #points do -- Loop to make the Mob walk to the points
            mob.Enemy.WalkToPoint = points[i]
            wait(speed)
        end
    --  anim:Stop()
    end
end

return MobAI
0
Hopefully this helped!! xXLegendarySoldierXx 129 — 8y
0
ty MHaven1 159 — 8y
Ad

Answer this question