#Pathfinding System

1 messages · Page 1 of 1 (latest)

tepid ridge
#

Hello, I am a beginner scripter and I'm making a pathfinding system and basically it just doesnt work even though i tried debugging it.

lone houndBOT
#

studio** You are now Level 3! **studio

tepid ridge
#
local NPC = script.Parent
local HumanoidRootPart = NPC.HumanoidRootPart
local pathfindingService = game:GetService("PathfindingService")

local MaxDistance = math.huge
local debounce = false

--> Damage

NPC.Humanoid.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not debounce then
        debounce = true
        hit.Parent.Humanoid:TakeDamage(20)
        task.wait(3.5)
        debounce = false
    end
end)

--> Detection

while task.wait() do
    local Players = game.Players:GetPlayers()
    local closest
    print("Begin")

    for i, plr in pairs(Players) do
        print("Loop1")
        if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health <= 0 then continue end
        print("Func1")
        
        local PlayerHumanoidRootPart = plr.Character.HumanoidRootPart
        local Distance = (HumanoidRootPart.Position - PlayerHumanoidRootPart.Position).Magnitude
        print("Distance Calculated")

        if closest and (HumanoidRootPart.Position - closest.Position).Magnitude < Distance then continue end
        print("Humanoid Detection with Distance")
        
        closest = PlayerHumanoidRootPart
        local path = pathfindingService:CreatePath()
        path:ComputeAsync(NPC.PrimaryPart.Position, closest.Position)
        print("Computed Path")
        

        for i, waypoint in pairs(path:GetWaypoints()) do
            print("Loop2")
            if not closest or (HumanoidRootPart.Position - closest.Position).Magnitude >= MaxDistance then continue end
            print("Distance 2")
            NPC.Humanoid:MoveTo(waypoint.Position)
            print("Moving")
            NPC.Humanoid.MoveToFinished:Wait()
            print("Waypoint finished")
        end
    end
end
#

It wont run

#

Like the loop prints itself weirdly

#

hold on

amber crystal
#

@tepid ridge

#

MaxDistance is inf so the distance check will never skip anything

lone houndBOT
#

studio** You are now Level 4! **studio

amber crystal
#

it might get paths from unnecessary locations

#

also your using NPC.Humanoid.MoveToFinished:Wait() inside the loop which blocks the entire script until the npc reached the waypoint

#

also you make a follow path for every player, not just the closest

#

change the ipairs for the closest player

for i, plr in pairs(Players) do
        if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health <= 0 then
            continue
        end

        local PlayerHRP = plr.Character:FindFirstChild("HumanoidRootPart")
        if not PlayerHRP then continue end

        local dist = (HumanoidRootPart.Position - PlayerHRP.Position).Magnitude
        if dist < closestDistance and dist <= MaxDistance then
            closest = PlayerHRP
            closestDistance = dist
        end
    end
#

also keep maxdistance somewhere reasonable

#

like 50

#

or 100

tepid ridge
#

yo yo

#

my bad

#

thank you bro

amber crystal
#

lemme explain it

#

better

#

currently this part

tepid ridge
#

mb

amber crystal
#

if closest and (HumanoidRootPart.Position - closest.Position).Magnitude < Distance then continue end

#

is saying "If the closest player found so far is closer than the current player, skip this player."

tepid ridge
#

yes

amber crystal
#

which should be the exact opposite

tepid ridge
#

ohh

#

i think its cuz i tried to do

#

uh

amber crystal
#

instead it should be skip if the current player is further than the closest found so far

tepid ridge
#

guard clause so my script isnt a harp

#

and i accientally

amber crystal
#

if closest and (HumanoidRootPart.Position - closest.Position).Magnitude > Distance then continue end

tepid ridge
#

changed a operator

amber crystal
#

try this instead

tepid ridge
#

okay

#

also

amber crystal
#

also you compute the path inside the player loop, but you don’t keep it or use it properly

#

You should first find the closest player in the entire loop, then only after the loop compute the path once to that closest player

tepid ridge
#

okay

#

so its placement isuses aswell

#

another huge issue is that it follows its point before it even tries to relocate and adapt to run at me

#

it will follow the waypoints

amber crystal
#

uhh

#

thats prob bc you don’t handle path status (success or failure) after ComputeAsync

#

You should check if path.Status == Enum.PathStatus.Success before moving

tepid ridge
#

i just dfixed i think

#

i removed the uh

#

movetofinished

#

thing u said

#

Oh

#

that mjust made the pathfinding stop

#

SO now it wont go around walls

#

Ye

#

that worked

amber crystal
#

anything else wrong?

tepid ridge
#

last thing is just

#

it stutters alot

#

when chasing me

#

oh vnm

#

the waypoint thing

#

is still delayed

amber crystal
#

oh uh

tepid ridge
#

the path computing is still in the loop btw

#

im not too sure how i can move it

amber crystal
tepid ridge
#

ok

#

Now it has prolonged sutters

amber crystal
#

alr remove the wait

tepid ridge
#

i think its what u said

#

i need to take

#

the compute

#

seperate from the loop

#

is there like a task.spawn or something i can do

#

so i dont gotta

#

move it

#

cuz that will eb ab it weird

lone houndBOT
#

studio** You are now Level 4! **studio

amber crystal
#

only compute after the closet player is found

tepid ridge
#

isnt it doing that here

amber crystal
#

yes but thats inside the loop

#

thats why ur getting issues

tepid ridge
#

so i need it out the for i loop

#

or the while

amber crystal
#

yes

#

for example

#

for _, plr in pairs(game.Players:GetPlayers()) do
if distance < shortestDistance then
shortestDistance = distance
closestPlayer = plr
end
end

#

THEN

#

if closestPlayer then

local path = pathfindingService:CreatePath()
path:ComputeAsync(NPC.PrimaryPart.Position, closestPlayer.Character.HumanoidRootPart.Position)

end

tepid ridge
#

ah

#

this closest assigning variable

#

isnt that versatile righjt now

amber crystal
#

well yes but its logic is in bad order

#

and there is no actual distance tracking

tepid ridge
#

yea

amber crystal
#

and even if there was there is no player reference saved

tepid ridge
#

i need a shortestDistance value

amber crystal
#

alright well them before the player loop define it

#

local closestPlayer = nil
local shortestDistance = math.huge

#

oh and findfirstchild hrp