How can I make a follower AI not jittery

My path is being updated constantly. This tutorial doesn’t seem to have anything about it.
Plus this was made in 2020 so the entire script is now using deprecated methods.
(my mistake, not 2020, 2022, yet still)

I just finished reading it, quite long. The part I’m referring to is under the “Bot AI Chasing” section. I suggest reading the whole thing but here’s the relevant bit:

Now that I’ve explained the reasons why, it’s time to create a new script. The flow of the script is simple. Everytime the while loop runs, it will call a function which tells the script to detect any closest player to the bot itself. If it managed to find one, we will tell the script to compute the path to it, and make our bot to only loop/walk through the second or third waypoint of the path before ending the function. Then this whole sequence would be repeated until our bot touches the player and makes the player die. Simple enough.

I personally haven’t really ran into an error where the AI chaser becomes jittery. In the zombie game I’m currently working on, the zombies may pause for a second when the player moves too fast, then just proceed as normal. It uses the same logic here (walk to the 2nd-3rd waypoint, then recalculate, repeat until all players dead). I’m not really sure what your case looks like, a video would be helpful.

Could you try to declare the Follow() function whenever the Closest Target moves? set a listener to the AssemblyLinearVelocity Property.

what

Can you explain better?

I’ve played around a bit, and I am satisfied with this script:

local Players = game:GetService("Players")
local Pathfinding = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")

local model = script.Parent
local primPart = model.PrimaryPart
local humanoid = model:WaitForChild("Humanoid")
humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

for _, v:Instance in ipairs(model:GetDescendants()) do
	if v:IsA("BasePart") then
		v:SetNetworkOwner(nil)
	end
end

local lastPosition = Vector3.new(math.huge, math.huge, math.huge)
local isStuck = false
local path:Path = Pathfinding:CreatePath({
	AgentCanClimb = true,
	Costs = {
		Climb = 2
	}
})
local obstructed = true
local function Follow(target:Model, distance:number)
	local root = target.PrimaryPart
	local hum:Humanoid = target:FindFirstChildWhichIsA("Humanoid")
	if root == nil or hum == nil then return end
	local destination = root.Position
	
	if not obstructed and not isStuck then
		humanoid:MoveTo(Vector3.new(destination.X, primPart.Position.Y, destination.Z))
	else
		local success, err = pcall(function()
			path:ComputeAsync(primPart.Position, destination)
		end)
		if success and path.Status == Enum.PathStatus.Success then
			local waypoints = path:GetWaypoints()
			if #waypoints < 2 then return end

			if not isStuck then
				local startWP:PathWaypoint = waypoints[1]
				local waypointsToGoThrough = {waypoints[2]}
				for i, wp:PathWaypoint in ipairs(waypoints) do
					if i <= 2 then continue end
					if (wp.Position - startWP.Position).Magnitude < 1 then
						table.insert(waypointsToGoThrough, wp)
					else
						break
					end
				end
				for i, wp in ipairs(waypointsToGoThrough) do
					humanoid:MoveTo(wp.Position)
				end
			else
				model:PivotTo(CFrame.new(waypoints[2].Position))
				isStuck = false
			end
		else
			if path.Status ~= Enum.PathStatus.NoPath then
				warn(`Error when trying to construct path between {model.Name} and {target.Name}:\n`..tostring(err))
			end
		end
	end
	
	--print((lastPosition-primPart.Position).Magnitude, isStuck)
	if (lastPosition-primPart.Position).Magnitude < 0.1 then
		isStuck = true
	else
		isStuck = false
	end
	lastPosition = primPart.Position
end

local function GetClosest(maxDist:number)
	local closest = nil
	local dist = maxDist
	for i, v in ipairs(workspace:GetChildren()) do
		if v:IsA("Model") then
			local rp:Part = v.PrimaryPart
			local plr = Players:GetPlayerFromCharacter(v)
			if rp and plr then
				local distance = (rp.Position - primPart.Position).Magnitude
				if distance < dist then
					dist = distance
					closest = v
				end
			end
		end
	end
	return closest, dist
end

RunService.Stepped:Connect(function(t, dt)
	local target:Model, dist = GetClosest(200)
	if target then
		local destination = target.PrimaryPart.Position
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {model, target}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		local direction = destination - primPart.Position
		local rayResult = workspace:Raycast(primPart.Position, direction, raycastParams)
		if rayResult == nil then
			obstructed = false
		else
			obstructed = true
		end
	else
		obstructed = true
	end
end)

task.spawn(function()
	while wait() do
		local targ:Model, dist:number = GetClosest(200)
		if dist > 20 then
			humanoid.WalkSpeed = 8
		else
			humanoid.WalkSpeed = 16
		end
		if not obstructed then
			humanoid.WalkSpeed += 4
		end
		if targ then
			Follow(targ, dist)
			if dist < 3 then
				local hum:Humanoid = targ:FindFirstChildWhichIsA("Humanoid")
				if hum and not targ:FindFirstChildWhichIsA("ForceField") then
					hum:TakeDamage(30)
					task.spawn(function()
						if hum.Health <= 0 then return end
						local speedIncrease = math.floor((hum.MaxHealth - hum.Health)/4 + 5)
						hum.WalkSpeed += speedIncrease
						--print(speedIncrease)
						
						local forcefield = Instance.new("ForceField")
						forcefield.Visible = false
						forcefield.Parent = targ
						forcefield.Destroying:Once(function()
							hum.WalkSpeed -= speedIncrease
						end)
						
						Debris:AddItem(forcefield, 2.5)
					end)
				end
			end
		end
	end
end)

He is so OP now. I regret calling him annoying. I can’t run away from him now. May he forgive me.
(He can’t jump, but in my game you won’t be able to jump either, so it is not necessary. You can add jumping ability if you’d like to.)

1 Like

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