Pathfinding movement acts strangely

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to create a monster pathfinding ai that can hear things and chase you when found.

  1. What is the issue? Include screenshots / videos if possible!

Everything works, except if the script is told to move multiple times the rig starts to “limp.” It makes it to the required target but with a ton of stopping in between.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried using debounce methods or some other ways to hopefully stall other calls, but it doesn’t seem to work. The script that I am about to show you may have some failed attempts at this.
I didn’t find any devhub or devforum article this obscure, so no.

local pathservice = game:GetService("PathfindingService")

local subject = script.Parent:FindFirstChild("Humanoid")
local repstorage = game:GetService("ReplicatedStorage")
local event = repstorage:WaitForChild("Noise")

local path = pathservice:CreatePath()
local aggro = false
local activect = nil
local heard = false

local NPC = script.Parent
local players = game.Players:GetPlayers()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local target = nil






--I hear you
--DevForum, For testing purpose the NPC "hears" you when I step on a part

event.OnServerEvent:Connect(function(plr, pos, range)
	if not heard then heard = true
		if (subject.Parent.HumanoidRootPart.Position - pos).Magnitude <= range then
			path:ComputeAsync(subject.Parent.HumanoidRootPart.Position, pos)
			local wp = path:GetWaypoints()
			table.remove(wp, 1)
			for i, v in pairs (wp) do
				if aggro then return end
				subject:MoveTo(v.Position)
				if v.Action == Enum.PathWaypointAction.Jump then
					subject.Jump = true
				end

				subject.MoveToFinished:Wait()
			end
			heard = false
		end
	end
	print("finished")
end)

--I see you
--DevForum, It keeps doing that strange movement pattern as long as I'm in sight,
--otherwise it's perfectly fine.

coroutine.wrap(function()
	print("OY")
	while true do
		wait(1/4)
		if aggro then
			update()
			repeat task.wait() until aggro == true
		end
	end
end)()

coroutine.wrap(function()
	while true do
		local players = game.Players:GetPlayers()
		for i, v in pairs (players) do
			local char = v.Character
			if not char then continue end
			local look = NPC.HumanoidRootPart.CFrame.LookVector   
			local facing_direction = (char.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position).Unit
			local dot = look:Dot(facing_direction)
			local angle = math.deg(math.acos(dot))

			if angle <= 80 then
				local rayresult = workspace:Raycast(NPC.HumanoidRootPart.Position, (char.HumanoidRootPart.Position - NPC.HumanoidRootPart.Position)*100) 
				if rayresult.Instance.Parent == char then
					aggro = true
					target = char.HumanoidRootPart.Position
				end 
			else
				aggro = false
				target = nil
			end
		end
		task.wait(1/4)
	end
end)()

function update()	
	path:ComputeAsync(subject.Parent.HumanoidRootPart.Position, target)
	local wp = path:GetWaypoints()
	table.remove(wp, 1)

	coroutine.wrap(startwalking)(wp)
end

function startwalking (wp)
	activect = coroutine.running()
	for i, v in pairs (wp) do
		if activect ~= coroutine.running() then return end
		subject:MoveTo(v.Position)
		print("moving")
		if v.Action == Enum.PathWaypointAction.Jump then
			subject.Jump = true
		end


		subject.MoveToFinished:Wait()
	end
end




This is the only script that you will need, its unnecessary to ask for the other scripts…

1 Like

what exactly do you mean by “limp”

It be better if you provide more detail

Add path Params. I had this problem and this fixed it:

local pathparams = {
["WaypointSpacing"] = math.huge, --this seemed to fix it but there are more things you can do
["AgentCanJump"] = boolean, --default is true
["AgentHeight"] = number, -- measured in studs, it won't hit its head if it can't go on a path
["AgentRadius"] = number --in studs, it won't hit the corner of a wall
}
--this is a new feature which idk if it has been added but it's default to false. It's: ["AgentCanClimb"]

Put the pathparams variable in the :CreatePath()
Also I’m not sure if you did it but also do:

Script.Parent.PrimaryPart:SetNetworkOwnership(nil) --this has to be the monster's model. It sets it to server so it doesn't stop randomly.
1 Like

I was wondering what SetNetworkOwnership does, thanks alot!

1 Like

You’re welcome! Charrrrrrrrrrrr

1 Like

SetOwnerblahblah switches between client and the server to reduce lag. But in AI, this switch will cause the monster to stop for a second. Setting it to nil will set it to the server and the switch won’t happen. There’s no workarounds. Doesn’t even cause that much lag.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.