#why is the rig just trying to go straight to me through the wall instead of following pathfinding

1 messages · Page 1 of 1 (latest)

timid field
#

i can share code if necessary but its trying to go directly to the humanoid root part instead of the waypoint its programmed to go to

i think i sort of have an idea on why but id like some help pinpointing it in my code and perhaps fixing it, thank you

pale dawn
#

its called "scripting-help"

timid field
#

playing deltarune though give me a bit

#
local RunServ = game:GetService("RunService")
local PathServ = game:GetService("PathfindingService")

local bonnie = script.Parent
local bonnieHumanoid = bonnie.Humanoid
local bonnieHrp = bonnie.HumanoidRootPart

bonnieHumanoid.WalkSpeed = 65

local path = nil
local waypoints = nil
local range = 300

local function CreatePlayerPath(target)
    local playerPath = PathServ:CreatePath()
    local targetHrp = target.Character:WaitForChild("HumanoidRootPart")
    
    local success, errorMessage = pcall(function()
        playerPath:ComputeAsync(bonnieHrp.Position, targetHrp.Position)
    end)
    
    if not success or playerPath.Status ~= Enum.PathStatus.Success then
        return nil
    end
    
    return playerPath
end

local function getClosestPlayer()
    local magnitudes = {}
    local target = nil
    
    for _,player in pairs(Players:GetPlayers()) do
        local dist = player:DistanceFromCharacter(bonnieHrp.Position)
        
        if dist <= range then
            table.insert(magnitudes, {dist, player})
        end
    end
    
    table.sort(magnitudes, function(a, b)
        return a[1] < b[1]
    end)
    
    if #magnitudes > 0 then
        target = magnitudes[1][2]
    end
    
    print("Target is:", target)
    return target
end

while true do
    local target = getClosestPlayer()
    
    if target then
        repeat task.wait() until target.Character or target.CharacterAdded:Wait()
        path = CreatePlayerPath(target)
        
        if path then
            waypoints = path:GetWaypoints()
            for _, waypoint in pairs(waypoints) do
                local part = Instance.new("Part")
                part.Position = waypoint.Position
                part.Size = Vector3.new(0.5, 0.5, 0.5)
                part.Color = Color3.fromRGB(84, 60, 165)
                part.Anchored = true
                part.CanCollide = false
                part.Parent = workspace
                
                game.Debris:AddItem(part, 0.5)
                
                bonnieHumanoid:MoveTo(waypoint.Position)
            end
        end
    else
        bonnieHumanoid:MoveTo(bonnieHrp.Position)
        path = nil
        waypoints = nil
    end
    RunServ.Heartbeat:Wait()
end```
#

@frosty bramble @pale dawn

#

i figured the reason was that it ran over and over again towards the rootpart of the player and it just didnt have time to follow the waypoint itself

frosty bramble
#

the problem with your code is that it restarts the path every single frame

#

you can make bonnie wait until she actually reaches each waypoint before moving to the next one

#

and you can also add a small pause between each path

timid field
timid field
pale dawn
#

i have never used it
i just found it in the docs

#

idk if it will work

timid field
#

but maybe i could tinker a little bit more

#

because i just put it right after the MoveTo part

pale dawn
#

the script is too long to send or smt

safe edgeBOT
#

studio** You are now Level 8! **studio

pale dawn
#

after bonnieHumanoid:MoveTo(waypoint.Position)

add bonnieHumanoid.MoveToFinished:Wait()

pale dawn
timid field
#

bonnie just moved to where i was

#

not where i am

pale dawn
#

i dont understand

#

wait i do now

timid field
#

yeah you see?

pale dawn
# timid field yeah you see?
while true do
    local target = getClosestPlayer()

    if target then
        repeat task.wait() until target.Character or target.CharacterAdded:Wait()
        local playerHrp = target.Character:WaitForChild("HumanoidRootPart")
        local lastPos = playerHrp.Position

        path = CreatePlayerPath(target)

        if path then
            waypoints = path:GetWaypoints()

            for _, waypoint in pairs(waypoints) do
                if (playerHrp.Position - lastPos).Magnitude > 5 then
                    break
                end

                local part = Instance.new("Part")
                part.Position = waypoint.Position
                part.Size = Vector3.new(0.5, 0.5, 0.5)
                part.Color = Color3.fromRGB(84, 60, 165)
                part.Anchored = true
                part.CanCollide = false
                part.Parent = workspace

                game.Debris:AddItem(part, 0.5)

                bonnieHumanoid:MoveTo(waypoint.Position)
                bonnieHumanoid.MoveToFinished:Wait()
            end
        end
    else
        bonnieHumanoid:MoveTo(bonnieHrp.Position)
        path = nil
        waypoints = nil
    end

    RunServ.Heartbeat:Wait()
end
#

it breaks away when you move too much and re calcs the path

#

@timid field does that work for you

timid field
#

oh thats interesting

#

cant run it rn but i will certainly check it out

timid field
#

it worked perfectly, thank you

#

@pale dawn

#

he is constantly stopping but its fine

pale dawn
timid field
#

yeah i figured

#

i spent some time yesterday trying to figure out how i can optimize it

#

but i sorta just gave up

#

might see what i can do later today though

pale dawn
#

also (playerHrp.Position - lastPos).Magnitude > 5
you could change the number 5 to something bigger but the bigger will number cause the rig to go to a more previous position