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

Pathfinding doesn't work when finish point is above starting point... Why?

Asked by 7 years ago

This started a while ago when I was using the old pathfinding (not the new one that was just released today) where a path just wasn't generated when the finish point was somewhat above the starting point. I thought the updated pathfinding would fix this problem, but it's still the same. I have zombies that chase you, but they just stop when you jump or you're above them. Here's the code, check it out and please help me figure out what's wrong!

wait()
local enemies = script.Parent.EnemyFolder.Value

function findNearestTorso(pos)
    local list = enemies:GetChildren()
    local torso = nil
    local dist = 1024
    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) and game.Players:GetPlayerFromCharacter(temp2) 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 wait() do
    if script.Parent ~= nil then
        local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
        if target then
            local path = game:GetService("PathfindingService"):FindPathAsync(script.Parent.HumanoidRootPart.Position, target.Position)
            local waypoints = path:GetWaypoints()
            if #waypoints > 2 then
                script.Parent.Humanoid:MoveTo(waypoints[3].Position)
                if waypoints[3].Action == Enum.PathWaypointAction.Jump then
                    script.Parent.Humanoid.Jump = true
                end
            elseif #waypoints >= 1 then
                script.Parent.Humanoid:MoveTo(waypoints[2].Position)
                if waypoints[2].Action == Enum.PathWaypointAction.Jump then
                    script.Parent.Humanoid.Jump = true
                end
            end
        end
    end
end
0
You only index 3 and 2 in the waypoints array? User#5423 17 — 7y
0
It's going off of an old script someone helped we with for an old game of mine for pathfinding zombies. They just said to use 2 and 3 so I just went with it and it worked. Then roblox did something a while ago and broke the pathfinding when I'm above the zombies. WesleyTheSkunk 12 — 7y
0
Stop doing complex calculations 33 times a second. Is it really necessary? Is everyone going at 100000000 studs per second that you need to update that fast? hiimgoodpack 2009 — 7y
0
This was back when I didn't have a clue about pathfinding and someone else helped me with it by telling me to do that. It worked then, but now roblox messed something up and I'm trying to figure out how to fix it. The speed of the loop is not the problem. WesleyTheSkunk 12 — 7y
View all comments (2 more)
0
When I comment on a question, it is called a suggestion, not an answer. hiimgoodpack 2009 — 7y
0
Sorry if this is off-topic but how does one find the ".Action" in an element in the "waypoints" array? Adrian12094 12 — 6y

Answer this question