IA Path is dont work correctly

Hi! I’m trying to make an AI that is able to go to random places and when it detects a player nearby it goes for him, but it gets buggy looking like it wants to go back to the old tracking, could you help me what would be the best mechanic?

The Code:

-- services
local pathfinding = game:GetService("PathfindingService")
local runservice = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")

-- variables
local debounce = false
local LastPoint = "none"
local Find = false
local debounce2 = false
local encontrar = false
local waiting = false

-- modelos
local balbu = script.Parent
local Humanoid = script.Parent.Humanoid
local Torso = script.Parent.HumanoidRootPart
local RunAnimation = script.Walk

-- animaciones
local AnimacionRUN = balbu:FindFirstChild("Humanoid"):LoadAnimation(RunAnimation)

-- remotes
local shake_remote = rs:FindFirstChild("Remotes"):FindFirstChild("Acciones"):FindFirstChild("ShakeCamera")


------- poner baseowner  -----------------
for _, object in pairs(script.Parent:GetChildren()) do 
	if object:IsA("UnionOperation") then 
		object:SetNetworkOwner()
	else if object:IsA("BasePart") then
			object:SetNetworkOwner()
		end
	end
end
------------------------------------------

AnimacionRUN.Looped = true
AnimacionRUN:AdjustSpeed(0.1)
AnimacionRUN:Play()

AnimacionRUN:GetMarkerReachedSignal("Golpe"):Connect(function(plr)
	local clonedmetal = script.Parent.Cuello.metal:Clone()
	clonedmetal.Parent = script.Parent.Cuello
	clonedmetal:Play()

	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr.Character then
			local tor = plr.Character:FindFirstChild("HumanoidRootPart")


			local magnitude = (script.Parent.Head.Position - tor.Position).Magnitude
			shake_remote:FireAllClients(plr, magnitude)

		end
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)

		runservice.Heartbeat:Connect(function(plr)
			local magnitude = (script.Parent.Head.Position - Character.Head.Position).Magnitude
			if magnitude >= 30 then
				Find = false
			else
				Find = true
			end
		end)
		
		
		local function Caminito(randChild)
			local ActiveID
			
			ActiveID = newproxy()
			
			local CurrentID = ActiveID
			
			local path = pathfinding:CreatePath({

				AgentRadius = 3.0,
				Costs = {
					DangerZone = 5,
					DontPass = math.huge
				}
			})

			path:ComputeAsync(Torso.Position, randChild.Position)
				
				local waypoints = path:GetWaypoints()
		
				for i, waypoint in pairs(waypoints) do
					Humanoid:MoveTo(waypoint.Position)
					Humanoid.MoveToFinished:Wait()
					
					if ActiveID ~= CurrentID then
						break
					end
				
				end	

		end
		
		
		local function FollowPlayer(randChild)
			
			local ActiveID
			
			ActiveID = newproxy()
			
			local CurrentID = ActiveID
			
			local path = pathfinding:CreatePath({

				AgentRadius = 3.0,
				Costs = {
					DangerZone = 8,
					DontPass = math.huge
				}
			})

			path:ComputeAsync(Torso.Position, randChild.Position)
			
			
			local waypoints = path:GetWaypoints()

			for i, waypoint in pairs(waypoints) do
				Humanoid:MoveTo(waypoint.Position)
				Humanoid.MoveToFinished:Wait()
				
				if ActiveID ~= CurrentID then
					break
				end
				
			end	
				
			--end
		end

		runservice.Heartbeat:Connect(function(plr)
			if Find == false then
				local folder = script.Parent.Parent.puntos
				script.Parent.Humanoid.WalkSpeed = 8
				local randChild = folder:GetChildren()[math.random(1, #folder:GetChildren())]
				AnimacionRUN:AdjustSpeed(1)
				Caminito(randChild)
			else
				local player = game.Players:GetPlayerFromCharacter(Character)			
				if player.Find.Value == true then
					AnimacionRUN:AdjustSpeed(3)
					script.Parent.Humanoid.WalkSpeed = 16
					Caminito(Character.HumanoidRootPart)
				end
			end		
		end)
		
		workspace.Casa.SafesZones.Cuartos.Touched:Connect(function()
			local player = game.Players:GetPlayerFromCharacter(Character)

			player.Find.Value = false
		end)

		workspace.Casa.SafesZones.Cuartos.TouchEnded:Connect(function()
			local player = game.Players:GetPlayerFromCharacter(Character)

			player.Find.Value = true
		end)


	end)
end)

and video example whats happend:

I’m not too sure about why this might not be working, but you could try cloning a part and putting it’s position to each pathfinding marker to help debug where it’s trying to go.

Im pretty sure its because the AI have current 2 paths to go. It just loops it self so the first loops it goes to point a then it changed its mind and go to point B. And loops so it was stucking like that. (if im wrong im sorry)

I recommended you getting a pathfinding module script since its easier to use and it was just like 3 lines. I currently use this one

1 Like

Oh, it works, thank you so much!

1 Like