Pathfinding AI breaks at some point

Hello there! I am currently in the process of making a strategy game. I want to use pathfinding for my soldiers. Yet, for some reason after some time the pathfinding service just “breaks”. Video demonstration:

--ServerScript (Issue probably here)


game.ReplicatedStorage.UnitRemotes.MoveTo.OnServerEvent:Connect(function(plr, objectlist, pos)
	for i, char in pairs(objectlist) do
		print("that")
		spawn(function()
			local PathfindingService = game:GetService("PathfindingService")
			local hum = char:WaitForChild("Humanoid")
			local torso = char:WaitForChild("Torso")

			local path = PathfindingService:CreatePath()
			path:ComputeAsync(torso.Position, pos)
			local waypoints = path:GetWaypoints()

			for i, waypoint in pairs(waypoints) do
				local brick = Instance.new("Part")
				brick.Size = Vector3.new(1,1,1)
				brick.Anchored = true
				brick.Transparency = 0.5
				brick.CanCollide = false
				brick.Parent = workspace
				brick.Position = waypoint.Position
				brick.Name = "briko"
				local Ignore = {}

				for i, thing in pairs(workspace:GetChildren()) do
					if thing:FindFirstChild("Humanoid") then
						table.insert(Ignore, thing)
					end
				end
				
				hum:MoveTo(waypoint.Position)
				hum.MoveToFinished:Wait()
				--[[
				local ray = Ray.new(char.Torso.Position, (brick.Position - char.Torso.Position).Unit * 40)
				local part, pos = workspace:FindPartOnRayWithIgnoreList(ray, Ignore)
				if part then
					if part.Name == "briko" then
						hum:MoveTo(waypoint.Position)
						print("success")
					else
						hum:MoveTo(waypoint.Position)
						print("fail")
						hum.MoveToFinished:Wait()
					end
				else
					hum:MoveTo(waypoint.Position)
					print("fail")
					hum.MoveToFinished:Wait()
				end
				--]]
			end
		end)
	end
end)

I don’t really get it, what’s breaking specifically?

1 Like

I think I fixed it, but what’s breaking is after some time, the character starts to stop at every waypoint. (It didn’t do that before)

One thing to remember. MoveToFinished after you wait atleast 8 seconds everything should be working, yeah it happens rarely, but I’m not sure how you can fix that.

However, you can look here more about it: Humanoid | Documentation - Roblox Creator Hub

But anyways, I’m glad everything’s working.

1 Like

Yeah, I just used raycasting and got everything working, thanks for responding.

1 Like