My ai is like Jolting to waypoints

  1. Im making an AI that walks to waypoints and when it sees a player he walks to the player

  2. The problem is he is like jolting to the waypoints I’m using pathfinding and its making waypoints so the AI doesn’t get stuck on anything but like every 2 studs he stops for a second and it makes it not look natural but when he sees a player, he doesn’t do it at all he moves smooth even though they both use the same pathfinding???
    this is a vid of what the problem is the bot is like jolting the waypoints.
    robloxapp-20230617-1929475.wmv (3.9 MB)

  3. I tried a lot I tried removing the recasting every waypoint that didn’t change anything I tried a different method of seeing when the bot finished moving to the waypoint, but nothing I tried fixed the problem.

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
wait(3)
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local waypointsFolder = script.Parent.Parent.waypoints
local waypoints = waypointsFolder:GetChildren()
local chasingPlayer = false
local FollowPlayerDuration = 2

local function FollowPlayer(char)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local plrhumanoid = char:WaitForChild("Humanoid")
	local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
	local second = FollowPlayerDuration
	
	
	while chasingPlayer do 
		local path = PathfindingService:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = true,
			Costs = {
				Slate = 20
			}
		})
		path:ComputeAsync(character.PrimaryPart.Position, HumanoidRootPart.Position)
		if second == 0 then
			local raycastResult = workspace:Raycast(script.Parent.Head.Position, char.HumanoidRootPart.Position - script.Parent.Head.Position, raycastParams)
			if raycastResult and game.Players:FindFirstChild(raycastResult.Instance.Parent.Parent.Name) or game.Players:FindFirstChild(raycastResult.Instance.Parent.Name) then
				chasingPlayer = true
				FollowPlayer(char)
			else
				chasingPlayer = false
				break
			end
		else 
			--This is where the AI is gunna chase the player
			print(second)
			if path.Status == Enum.PathStatus.Success then
				for i, v in pairs(path:GetWaypoints()) do
					humanoid:MoveTo(v.Position)
				end
			else
				print("Cant get to player")
				break
			end

			
		end
		
		wait(0.05)
		if second > 0 then
			second -= 0.05
		else
			second = 0
		end
		
	end
end

local function seePlayer(zombieHeadPosition)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	for i, v in pairs(game.Players:GetChildren()) do
		local char = game.Workspace:FindFirstChild(v.Name)
		if char and char:FindFirstChild("HumanoidRootPart") then
			local raycastResult = workspace:Raycast(zombieHeadPosition, char.HumanoidRootPart.Position - zombieHeadPosition, raycastParams)
			if raycastResult and game.Players:FindFirstChild(raycastResult.Instance.Parent.Parent.Name) or game.Players:FindFirstChild(raycastResult.Instance.Parent.Name) then
				chasingPlayer = true
				FollowPlayer(char)
			end
		end
	end
end


local function moveToNextWaypoint()
	if #waypoints > 0 then
		local currentWaypointIndex = math.random(1, #waypoints)
		local waypoint = waypoints[currentWaypointIndex]

		local path = PathfindingService:CreatePath({
			AgentRadius = 2,
			AgentHeight = 6,
			AgentCanJump = true,
			Costs = {
				Slate = 20
			}
		})
		path:ComputeAsync(character.PrimaryPart.Position, waypoint.Position)

		if path.Status == Enum.PathStatus.Success then
			for i, v in pairs(path:GetWaypoints()) do
				seePlayer(script.Parent.Head.Position)
				humanoid:MoveTo(v.Position)
				humanoid.MoveToFinished:Wait()
			end
			
		else
			-- If pathfinding fails, find a new waypoint
			print("There was a problem")
			moveToNextWaypoint()
		end
	end
end

while wait() do
	print("NextPoint")
	moveToNextWaypoint()
end