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

Pathfinding algorithm will not work and it is stuck in a place? help

Asked by 2 years ago

So recently I wanted to impletment a pathfinding system from scratch but I can't get it to work -.It uses the a star pathfinding algorithm. the pivot is stuck in one place.- It seems like the pivot got blocked by evaluated points squares - i give up , - im also a beginner so im bad ,heres the code:

-results : https://imgur.com/a/PdlypjY

can someone please help

function searchPath(start,goal,moveSpan,filteredParts)
    local function getDistance(x1,y1,x2,y2) -- refer y to z (CF)
        return math.sqrt ( math.pow ( x2 - x1, 2 ) + math.pow ( y2 - y1, 2 ) )
    end
    local skip = 0
    local open = {}
    local closed = {}
    local fcost = {}
    local gcost = {}
    local paths = {}
    local path = Instance.new("Part",script)
    path.Name = "path"
    path.CFrame = start
    path.Size = Vector3.new(moveSpan,moveSpan,moveSpan)
    path.Anchored = true
    path.BrickColor = BrickColor.new(37)
    local endingGoal = Instance.new("Part",script)
    endingGoal.Name = "goal"
    endingGoal.CFrame = goal
    endingGoal.BrickColor = BrickColor.new(21)
    endingGoal.Size = Vector3.new(moveSpan,moveSpan,moveSpan)
    endingGoal.Transparency = 0.4
    endingGoal.Anchored = true
    for i = 1,1 do
        table.insert(open,path.CFrame)
        local widedede = getDistance(path.CFrame.X,path.CFrame.Z,goal.X,goal.Z)
        table.insert(fcost,widedede)
        table.insert(gcost,0)

    end
    local currentNode = path.CFrame
    repeat

        wait()
        local prev = path.CFrame
        print(#open," ",#closed)
        if skip ~= 1 then
            local ensure = 0
            local auxList = fcost
            table.sort(auxList, function(a, b) return a < b end)
            local currentNode = auxList[1]
            for i,v in pairs(fcost) do
                if fcost[i] == currentNode then
                    table.insert(closed,open[i])
                    ensure = open[i]
                    table.remove(open,i)
                    table.remove(fcost,i)
                    table.remove(gcost,i)
                end
            end
            currentNode = ensure
            path.CFrame = currentNode
        end
        path.CFrame = currentNode
        if currentNode == goal then
            print("Goal reached [2] - ",path.CFrame)
            break
        end
        print("bruh")
        local currentCHg = getDistance(currentNode.X,currentNode.Z,start.X,start.Z)
        skip = 0
        local function getChildsOFobj(objPATH,fromCF,times,mSpan,from)
            --objPATH.Name = "yessir"
            --objPATH.Size = Vector3.new(moveSpan,moveSpan,moveSpan)
            --objPATH.Anchored = true
            local dir = 0
            objPATH.CFrame = fromCF
            for i=1,times do
                wait()
                dir += 1
                print(dir)
                if dir == 1 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                elseif dir == 2 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                elseif dir == 3 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                elseif dir == 4 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                elseif dir == 5 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                elseif dir == 6 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                elseif dir == 7 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                elseif dir == 8 then
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                    objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                end
                print(open,"|",closed)
                local allowLoop = 0
                local sus = objPATH:Clone()
                sus.Parent = script
                sus.Transparency = 0.4
                sus.BrickColor = BrickColor.new(6)
                sus.Name = "TRACER"
                allowLoop = 0
                for i,v in pairs(closed) do
                    if closed[i] == objPATH.CFrame then
                        allowLoop = 2

                    end
                end

                local curTemp  = getDistance(objPATH.CFrame.X,objPATH.CFrame.Z,currentNode.X,currentNode.Z)
                local currentGch = currentCHg + moveSpan
                local temp = getDistance(objPATH.CFrame.X,objPATH.CFrame.Z,goal.X,goal.Z)
                local currentFch = currentGch + temp
                if allowLoop ~= 1 then
                    for i,v in pairs(open) do
                        wait()
                        if open[i] == objPATH.CFrame then
                            print(open[i])
                            local auxiList = gcost
                            table.sort(auxiList, function(a, b) return a < b end)
                            local min = auxiList[1]
                            if currentGch > min or currentGch >= min then
                                fromCF = prev               
                                allowLoop = 1

                            end
                        end
                        if allowLoop == 0 then
                            table.insert(open,objPATH.CFrame)
                            table.insert(gcost,currentGch)
                            table.insert(fcost,currentFch)
                        end
                    end


                    wait()
                    print("bruh")
                    if dir == 1 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                    elseif dir == 2 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                    elseif dir == 3 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                    elseif dir == 4 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                    elseif dir == 5 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                    elseif dir == 6 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(mSpan,0,0)
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                    elseif dir == 7 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,mSpan)
                    elseif dir == 8 then
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(-mSpan,0,0)
                        objPATH.CFrame = objPATH.CFrame * CFrame.new(0,0,-mSpan)
                    end
                end

            end

            objPATH.CFrame = fromCF
        end
        getChildsOFobj(path,currentNode,4,moveSpan,currentNode)

    until #open < 1
    print("DONE [4]")
    print(path.Position)
    if path.CFrame ~= goal then
        warn("Failed in finding path")
        print(path.Position)
    elseif path.CFrame == goal then
        print("Sucess in finding path")
        print(path.Position)
    end
    return paths
end
wait(3)
local paths = searchPath(CFrame.new(0,0,0),CFrame.new(15,0,1),1)

2 answers

Log in to vote
0
Answered by 2 years ago

Try using this:

local NPC = script.Parent
local PathFinding = game:GetService("PathfindingService")
local Target = game.Workspace.Location

while wait() do
    local Path = PathFinding:CreatePath()
    Path:ComputeAsync(NPC.Torso.Position, Target.Position)
    if Path.Status == Enum.PathStatus.Success then
        local Waypoints = Path:GetWaypoints()
        for i,v in pairs(Waypoints) do
            local Part = Instance.new("Part", workspace)
            Part.CanCollide = false
            Part.Anchored = true
            Part.Transparency = 1
            Part.Position = v.Position
            NPC.Humanoid:MoveTo(Part.Position)
            NPC.Humanoid.MoveToFinished:Wait()
            Part:Destroy()
        end
    end
end

I also made a model If you want to use that Instead: https://www.roblox.com/library/10167810365/

(Also Ignore the tags, Its just Innocent words tagged by roblox)

0
No, i meant like a custom one aside from roblox's algorithm. Cause i want to make a custom one for my 2D gui based game on roblox yongshinleo5 0 — 2y
0
Oh, well sorry, Im not good at GUI, sorry If this doesn't work imnotaguest1121 362 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

after checking and fixing it several times i got it to work!

Answer this question