Monster Stutters When Chasing Player

Hello! I Am Making A Horror Game Monster And There Is A Glitch(?) That Is A Bit Annoying Where The Monster Just Starts Stuttering And Not Chasing Smoothly, I Cant Really Explain More So I’ll Just Send A Video.


Video Of The Glitch :


AI Script :

local VDI = script.Parent
local Humanoid = VDI.Humanoid
local PathFindingService = game:GetService("PathfindingService")
VDI.PrimaryPart:SetNetworkOwner(nil)

local timeout = 0
local maxTimeout = 100 --How many times it checks before deciding to proceed anyways
local nextPointDistance = 2 --How far VDI has to be before going to next point

local function CanSeeTarget(Target)
	local origin = VDI.HumanoidRootPart.Position
	local direction = (Target.HumanoidRootPart.Position - VDI.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, VDI)
	
	if hit then
		if hit:IsDescendantOf(Target) then
			return true
		end
	else
		return false
	end
end

local function FindTarget()
	local Players = game.Players:GetPlayers()
	local MaxDistance = 75
	local NearestTarget = nil

	for index, player in pairs(Players) do
		if player.Character then
			local Target = player.Character
			local Distance = (VDI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude

			if Distance < MaxDistance and CanSeeTarget(Target) then
				NearestTarget = Target
				MaxDistance = Distance
			end
		end
	end

	return NearestTarget
end

local function GetPath(destination)
	local PathParms = {
		["AgentHeight"] = 8,
		["AgentRadius"] = 6,
		["AgentCanJump"] = false
	}

	local Path = PathFindingService:CreatePath(PathParms)

	Path:ComputeAsync(VDI.HumanoidRootPart.Position, destination.Position)

	return Path
end

local function Attack(Target)
	local Distance = (VDI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude

	if Distance > 8 then
		Humanoid:MoveTo(Target.HumanoidRootPart.Position)
		Humanoid.WalkSpeed = 32
	else
		Target.Humanoid.Health = 0
	end
end

local function WalkTo(destination)
	local Path = GetPath(destination)

	if Path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(Path:GetWaypoints()) do
			local Target = FindTarget()

			if Target and Target.Humanoid.Health > 0 then
				Attack(Target)
				break
			else
				Humanoid.WalkSpeed = 16
				Humanoid:MoveTo(waypoint.Position)
				repeat
					task.wait(0.05) --You can change this to whatever to increase or decrease checking speed
					timeout += 1
				until timeout >= maxTimeout or (VDI.PrimaryPart.Position - Vector3.new(0, 5, 0) - waypoint.Position).Magnitude <= nextPointDistance
				timeout = 0
			end
		end
	else
		Humanoid:MoveTo(destination.Position - (VDI.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function Patrol()
	local WayPoints = workspace.WayPoints:GetChildren()
	local RandomNum = math.random(1, #WayPoints)

	WalkTo(WayPoints[RandomNum])
end

while task.wait(0.5) do	
	Patrol()
end

If i under it correctly this is the number that messes everything up, try setting it to 1.

the Distance 8 or AgentHeight 8?

The distance 8

Putting this here because i need text: aaaaaaaaaaaaaaaaaaaaaaaaaa

it is still the same. did not change anything.

The reason it is stuttering is because it is not taking the most recent player’s position.

You could fix this in two ways:

  1. Change while task.wait(0.5) do to while task.wait() do
  2. Try and predict where the player is going to move next, but this will be a longer process.
1 Like

Wow, You Are Very Smart, Thanks! Now The Ai Is Done! I just Need Some little stuff and i can put it in the full game! woo

1 Like

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