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

I am making a piggy type of game and Ai pathfinding script broken. How can I fix it?

Asked by 3 years ago
Edited 3 years ago

I am making a piggy form of game and my Ai pathfinding script is not working any way I can fix it? Here the script:

local Scientist = script.Parent
local humanoid = Scientist.Humanoid

local PathfindingService = game:GetService ("PathfindingService")

local function getPath(destination)
    local path = PathfindingService:CreatePath()

    path:ComputeAsync(Scientist.HumanoidRootPart, destination.Position)

    return path
end 

local function walkTo(destination)
    local path = getPath(destination)

    for index, waypoint in pairs(path:GetWaypoints()) do
        humanoid:MoveTo(waypoint.Position)
        humanoid.MoveToFinished:Wait()
    end
end

while true do
    walkTo(workspace.destination)
    walkTo(workspace.destination2)
end
0
Can you please put everything inside of a lua block? It's really hard to read and fix it this way. ColdFoxy07 76 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Try This

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 100000 -- distance you want away from the player
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end




while true do
    wait(math.random(1,5))
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end

end

Ad

Answer this question