Hello, I made pathfinding for a monster and it really sucks. Can you guys please help fix my code because I don’t know what else to do.
I will help you
I can try to help
don’t know
0voters
Here are some big issues with the pathfinding that we need to fix
its really laggy and the monster keeps running in place
the monster keeps running into the wall
the monster slows down as it aproaches me
Here is my code
local pathfindingService = game:GetService("PathfindingService")
local path = pathfindingService:CreatePath()
while true do
local success,failure = pcall(function()
path:ComputeAsync(plrRoot.Position, HRP.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoint = path:GetWaypoints()
hum:MoveTo(waypoint[1].Position)
hum.MoveToFinished:Wait()
end
end
You are creating whole new path everytime the monster reaches its waypoint. Literally anything you need is in the roblox documentation here
Please do some research before posting.
I created this script after looking at that roblox documentation. The reasoning for creating a whole new path is because the monster is chasing the player and I don’t want the monster walking in the area the player was previously
This is not pathfinding fault bruh. He does not even loop through the waypoints. He just goes to the first one and then calculates it again. Roblox pathfinding is good if you know how to implement it
That is not how you should do it, check how far the player is from the last waypoint, if the distance is more than, let’s say, 10 studs, break the loop and make it create a new path
If the monster is walking long distance, let’s say more than 10 studs, just let it go through the path. And if it’s close to the enemy, just use hum:WalkTo() with raycasting to check if there’s an obstacle
It will definitely fix the lagging problem aka the third one. Walking in place is not pathfinding fault but you forgot to stop the animation somewhere.
local function chase(plr)
local plrChar = plr.Character
local plrRoot = plrChar.HumanoidRootPart
chasing = true
run:Play()
task.spawn(gotAway, plr)
while chasing do
local success,failure = pcall(function()
path:ComputeAsync(plrRoot.Position, HRP.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoint = path:GetWaypoints()
for i,v in pairs(waypoint) do
local distance = (waypoint[#waypoint].Position - plrRoot.Position).Magnitude
if distance < 10 then
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
break
end
end
end
end
run:Stop()
end
For problem # 3, the solution is setting it’s network ownership to nil, aka server, as it get’s closer, it the game engine keeps switching it’s network ownership between the nearest player, aka you, and the server, this causes it to jitter/lag for a second, this is what’s causing it to be slow as it approaches you
hello you guys said I should loop through and break the loop if the monster is too far however I did that and it does this
here is the current script:
local pathfindingService = game:GetService("PathfindingService")
local path = pathfindingService:CreatePath()
while chasing do
local success,failure = pcall(function()
path:ComputeAsync(plrRoot.Position, HRP.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoint = path:GetWaypoints()
for i,v in pairs(waypoint) do
local distance = (waypoint[#waypoint].Position - plrRoot.Position).Magnitude
if distance < 10 then
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
else
break
end
end
end
end
something to note is that I did
local distance = (waypoint[#waypoint].Position - plrRoot.Position).Magnitude
print(distance)
Because you are breaking the loop unless the distance is smaller than 10. Instead the break statement, you need to walk through the waypoints, and in the if condition put :MoveTo(playerposition)
Isn’t the whole point of this to break the for loop and generate a new point in the case that the player has ventured too far away from the previously generated path.
The 31.8444 magnitude difference, try moving around, does it change, just stand still when it generates a new path, is the distance difference still so great