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

Why is my npc just running into a wall even when he generates a path?

Asked by 4 years ago
Edited 4 years ago

This may be a big ask but can some one look at this code and tell me why the npc is just walking into the wall even tho he generates the path and I tell him to move along the waypoints.

--Services
local PathFinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
--Variables
local info = script.Parent
local owner = info:WaitForChild("Owner").Value
local character = script.Parent.Parent.Parent
local charRoot = character.HumanoidRootPart
local followRoot = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local followHumanoid = script.Parent.Parent:WaitForChild("Humanoid")

followRoot.CFrame = charRoot.CFrame + charRoot.CFrame.LookVector * -2

function checkSight()
    local ray = Ray.new(followRoot.Position, (charRoot.Position - followRoot.Position).Unit * 360)
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent.Parent})
    if hit then
        if math.abs(hit.Position.Y - followRoot.Position.Y) < 5 and math.abs(hit.Position.Y - followRoot.Position.Y) < 3 then
            print("I can see the followRoot")
            return true
        end
    end
    return false
end

function createPart(positon)
    local part = Instance.new("Part")
    part.Parent = game.Workspace
    part.Size = Vector3.new(1,1,1)
    part.Shape = "Ball"
    part.Anchored = true
    part.CanCollide = false
    part.Position = positon
    part.Material = "Neon"
    game:GetService("Debris"):AddItem(part,5)
end

function moveToWaypoints(path)
    local waypoints = path:GetWaypoints()
    for _,waypoint in pairs(waypoints) do
        print("Next Waypoint")
        createPart(waypoint.Position)
        if waypoint.Action == Enum.PathWaypointAction.Jump then
            followHumanoid.Jump = true
        end
        followHumanoid:MoveTo(waypoint.Position)
    end
end


function generatePath()
    --print("Generating Path")
    local path = PathFinding:CreatePath()
    path:ComputeAsync(followRoot.Position,charRoot.Position)
    if path.Status == Enum.PathStatus.Success then
        return path
    end
end

function main()
    repeat
        followHumanoid:MoveTo(charRoot.Position)
        RunService.Stepped:Wait(0.1)
    until not checkSight()
    local path = generatePath()
    if path ~= nil then
        moveToWaypoints(path)
    end
end

while RunService.Stepped:Wait(0.1) do
    main()
end


https://gyazo.com/a47aa6ddcd5f31100fae150f808e9469

Answer this question