Hey there, developers!
Recently, my nextbot-like chase game has randomly had this huge bug, where the entities don’t move occasionally. I checked the console, and what do you know? I get warnings that read:
“PathFindingService: Path too long.”
This seems like a normal error - but the caveat is that I am not far away from the entities at all. Infact, sometimes I’m less than 10 studs away from them, and I still get this warning thrown at me.
I don’t know if this is a ping issue (probably not, my game gets normal ping), or a roblox bug, or maybe my code is flawed?
I didn’t want to make a bug report about this - because again, it could be my code, but I didn’t touch the script at all.
Here’s the code for anybody wondering (messy, yes I know):
local ps = game:GetService("PathfindingService") -- get this service
local ts = game:GetService("TweenService")
local speed = 0.4
local info = TweenInfo.new(speed, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
repeat task.wait() until game:GetService("ContentProvider").RequestQueueSize == 0 -- wait until the game fully loads
local closestDistance = math.huge
local maxDistance = 500
local closestPlayer = nil
local target
local newPos = nil
local brickSignal = game.Workspace:WaitForChild("brickSignal")
task.wait(3)
local brickSpeed = 0.4
local FlyTween = ts:Create(script.Parent, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false), {Position = newPos})
local Direction = script.Parent.CFrame.LookVector
while task.wait() do
-- run everything below forever
--task.wait(5)
--local plrAmt = #game.Teams.Playing:GetPlayers()
--local brickAmount = math.floor(plrAmt / 4)
--if plrAmt >1 then
-- for i = 1,brickAmount,1 do
-- local clones = script.Parent:Clone()
-- clones.CFrame = script.Parent.CFrame * CFrame.new(math.random(-100,100),math.random(-100,100),math.random(-100,100))
-- if game.Workspace:FindFirstChild("MapFolder", true) then
-- script.Parent = game.Workspace.MapFolder
-- else
-- script.Parent = script.Parent.Parent
-- end
-- break
-- end
--end
if brickSignal.Value == 1 then
--task.wait(0.1) -- if you don't want to put too much pressure on the server, add a small cooldown
closestDistance = math.huge -- reset the closest distance so that we can find it over and over
closestPlayer = nil
for i,v in pairs(game.Players:GetPlayers()) do -- get every player in the game
if v and v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
local distance = (v.Character:FindFirstChild("HumanoidRootPart").Position - script.Parent.Position).Magnitude -- get every player's distances
--print(distance) -- debug
if distance <= closestDistance and distance and v.Character:FindFirstChildOfClass("Humanoid").Health > 0 and v.Team ~= game.Teams.Lobby then -- if the distance of the player is smaller than the previous closest distance, then run the code below
closestDistance = distance
humanoid = v.Character:FindFirstChild("Humanoid")
closestPlayer = v.Character:FindFirstChild("HumanoidRootPart")
--print(closestDistance.. " is the closest distance")
script.Parent.Orientation = closestPlayer.Orientation
end
end
end
if closestPlayer and humanoid and humanoid.Health > 0 then
--target = closestPlayer.Position + closestPlayer.CFrame.LookVector * humanoid.WalkSpeed / 2
--target = CFrame.new(script.Parent.Position, closestPlayer.Position).LookVector
target = closestPlayer.Position + closestPlayer.AssemblyLinearVelocity
elseif not closestPlayer or not (humanoid.Health > 0) or not humanoid or closestDistance > maxDistance then
target = script.Parent.Position
script.Parent.Position = script.Parent.Position
end
-- the target to pathfind to
local path = ps:CreatePath() -- create a path
local pathfind = nil
local yay, nay = pcall(function()
pathfind = path:ComputeAsync(script.Parent.Position, target) -- create a path between this npc's humanoid root part and the closest player's humanoid root part
end)
if nay then
warn(nay.." Distance: "..(script.Parent.Position - target).Magnitude)
--game.Chat:Chat(script.Parent, "better run", Enum.ChatColor.Red)
--script.Parent["Audio/stone_drag"].Playing = true
--FlyTween:Play()
pathfind = path:ComputeAsync(script.Parent.Position, script.Parent.Position)
end
for z,waypoint in pairs(path:GetWaypoints()) do -- get all of the waypoints
local newerPos = waypoint.Position
local bestTween = ts:Create(script.Parent, info, {Position = newerPos})
bestTween:Play()
FlyTween:Cancel()
--script.Parent.CFrame *= CFrame.new(Direction.X, Direction.Y, Direction.Z)
task.wait(math.clamp(brickSpeed, 0.05, 2)/closestDistance)
end
if path.Status == Enum.PathStatus.Success then
--print("i'm coming for you")
script.Parent["Audio/stone_drag"].Playing = false
FlyTween:Cancel()
else
if closestPlayer then
newPos = closestPlayer.Position
else
newPos = script.Parent.Position
end
local bestTween = ts:Create(script.Parent, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false), {Position = newPos})
--game.Chat:Chat(script.Parent, "better run", Enum.ChatColor.Red)
script.Parent["Audio/stone_drag"].Playing = true
bestTween:Play()
FlyTween:Cancel()
end
path.Blocked:Connect(function(b)
warn("path blocked")
end)
--if closestDistance <= 10 then
-- humanoid:TakeDamage(100)
--end
--end
end
end
Thank you developers, and have a nice day!