Chase script non-functional

Hello devforum, I have a script that I’ve been working on for a horror game that doesn’t function the way I want it to.

For context, the script is for a humanoid AI, and is mainly to allow it to chase after players when close enough. The main aspects I want to achieve with it is to have it pathfind around geometry so that it can properly reach players, but more notably, update the path frequently (about once every .1 seconds) to more accurately chase the player, since said player is most likely going to be moving, sometimes a lot.

However, the issue that arises is that the AI just doesn’t move at all, and in previous attempts to fix the script, it moves for .1 seconds, but then stops. There aren’t even any errors that appear in the output, the AI just completely breaks.

Despite my best efforts, the outcome has remained pretty much the same; works for a bit, but breaks not even a second later.

local function FindNearestPlayer()
	local chaseRadius = script.Parent.Configuration.ChaseRadius
	local nearestPlayer = nil
	local nearestDistance = math.huge
	for _, player in ipairs(game.Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local playerPosition = player.Character.HumanoidRootPart.Position
			local distance = (script.Parent.HumanoidRootPart.Position - playerPosition).Magnitude
			if distance <= chaseRadius.Value and distance < nearestDistance then
				nearestPlayer = player
				nearestDistance = distance
			end
		end
	end
	return nearestPlayer
end

local function Chase(nearestPlayer)
	if nearestPlayer then
		script.Parent.Humanoid.WalkSpeed = script.Parent.Configuration.RunSpeed.Value
		local path = game:GetService("PathfindingService"):CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = false, 
			AgentMaxSlope = 45,
		})
		path:ComputeAsync(script.Parent.HumanoidRootPart.Position, nearestPlayer.Character.HumanoidRootPart.Position)
		while path.Status ~= Enum.PathStatus.Success do
			task.wait(.1)
			return Chase(FindNearestPlayer())
		end
		for _, waypoint in ipairs(path:GetWaypoints()) do
			script.Parent.Humanoid:MoveTo(waypoint.Position)
		end
	end
end

local function Chasing()
	while true do
		local nearestPlayer = FindNearestPlayer()
		Chase(nearestPlayer)
	end
end

script.Parent.Head.ScreamA:Play()
script.Parent.Head.ScreamB:Play()
Chasing()

Any kind of help would be appreciated, as this issue has stumped me for quite some time now.
1 Like

When this function is fired it doesn’t stop.

local function Chasing()
	while true do
		local nearestPlayer = FindNearestPlayer()
		Chase(nearestPlayer)
	end
end

You could put prints into the script in locations to show the variables before each step in the script to show you which sections of code are being run. Something like this in this section of code:

local function Chase(nearestPlayer)
    print("nearestPlayer is ", nearestPlayer.Name)
	if nearestPlayer then
    -- code

The AI is meant to stop chasing once it goes out of range (this is because of an operator script that is outside the chase script, and it just disables the chase script once the AI is out of range)

As for the issue itself, I’m pretty certain it has to do with these lines

path:ComputeAsync(script.Parent.HumanoidRootPart.Position, nearestPlayer.Character.HumanoidRootPart.Position)
		while path.Status ~= Enum.PathStatus.Success do
			task.wait(.1)
			return Chase(FindNearestPlayer())
		end
		for _, waypoint in ipairs(path:GetWaypoints()) do
			script.Parent.Humanoid:MoveTo(waypoint.Position)
		end

The rest of the code works perfectly fine, especially with the oldest version of the chase script in which the AI would just beeline (run) straight for the nearest player, but now that I’ve added this stuff, the AI just doesn’t work as intended.