#Pathfinding System
1 messages · Page 1 of 1 (latest)
** You are now Level 3! **
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
** You are now Level 4! **
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
What should i use?
mb
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."
yes
which should be the exact opposite
instead it should be skip if the current player is further than the closest found so far
if closest and (HumanoidRootPart.Position - closest.Position).Magnitude > Distance then continue end
changed a operator
try this instead
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
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
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
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
anything else wrong?
last thing is just
it stutters alot
when chasing me
oh vnm
the waypoint thing
is still delayed
oh uh
add a wait(1) right before the last end
alr remove the wait
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
** You are now Level 4! **
you gotta change your start so that it finds the closest player first
only compute after the closet player is found
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
yea
and even if there was there is no player reference saved
i need a shortestDistance value