Error: "Script timeout: exhausted allowed execution time" with player targeting npc

Hello, I’m trying to create an npc that targets the nearest player, but I end up with this error: “Script timeout: exhausted allowed execution time”. I haven’t found any solutions currently, and any help would greatly be appreciated. This is my script:

local chars = {}
local magnitudes = {}

local ps = game:GetService("PathfindingService")
local human = script.Parent.Humanoid
local hrp = script.Parent.HumanoidRootPart
local currenttarget = nil

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		table.insert(chars, char)
		for i, char in pairs(chars) do
			for i, magnitude in pairs(magnitudes) do
				table.remove(magnitudes, table.find(magnitudes, magnitude))
			end
			table.insert(magnitudes, (char.HumanoidRootPart.Position - hrp.Position).Magnitude)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.CharacterRemoving:Connect(function(char)
		table.remove(chars, table.find(chars, char))
		for i, char in pairs(chars) do
			for i, magnitude in pairs(magnitudes) do
				table.remove(magnitudes, table.find(magnitudes, magnitude))
			end
			table.insert(magnitudes, (char.HumanoidRootPart.Position - hrp.Position).Magnitude)
		end
	end)
end)

repeat
	for i, magnitude in pairs(magnitudes) do
		currenttarget = chars[table.find(chars, table.find(magnitudes, magnitude))]
		local path = ps:CreatePath()
		path:ComputeAsync(hrp.Position, currenttarget.HumanoidRootPart)
		local waypoints = path:GetWaypoints()
		for i, waypoint in pairs(waypoints) do
			human:MoveTo(waypoint.Position)
			human.MoveToFinished:Wait()
		end
		human:MoveTo(currenttarget.HumanoidRootPart.Position)
	end
until false

Thanks for reading this!

I would suggest adding a task.wait() inside the repeat loop to stop this error.

1 Like

also try checking if workspace.StreamingEnabled is disabled.

Streaming causes a lot of scripts to exhaust

which line would i put that in?

its already disabled (cHaaRrRrs)

You can add it right after the Repeat statement, or right before the until statement

That got rid of that error, but now I have to deal with this one: "Workspace.Dummy.Script:37: attempt to index nil with ‘HumanoidRootPart’

do a print or debug of currenttarget and see what it’s outputting, it sounds like that value is being provided as nil

this is the problem, as magnitudes cannot be found in this table, but i struggle to find the solution

You need a task.wait() in your repeat so it waits a very small bit before looping again.

repeat task.wait()
		currenttarget = chars[table.find(chars, table.find(magnitudes, magnitude))]
		--the rest of your code

I got rid of that error, it’s this that’s the problem: Error: "Script timeout: exhausted allowed execution time" with player targeting npc - #9 by Berrinlin

I think you’re using table.remove() incorrectly. table.remove() needs the table and the position of what you want to remove. table.remove(table, 1)

Just do:

currenttarget = chars[table.find(chars, i)]

that wouldnt work.
i is a number value, and chars is a table that would only hold characters.

currenttarget = chars[table.find(chars, table.find(magnitudes, magnitude))]

apparently, i just had to change this to: “currenttarget = chars[i]”

Since you are struggling with getting this to work, try this:

local playerService = game:GetService("Players")

local function getPlayerNearest(npcModel)
	local minDist = math.huge
	local minPlayer = nil
	local npcHRP = npcModel:FindFirstChild("HumanoidRootPart")
	if npcHRP == nil then
		-- Invalid NPC model.
		return nil
	end

	local players = playerService:GetPlayers()
	for _, player in pairs(players) do
		local playerHRP  = player.Character:FindFirstChild("HumanoidRootPart")
		if playerHRP == nil then
			-- Invalid player model.  Did they log off?
			return nil
		end
		local dist = (npcHRP.Position - playerHRP.Position).Magnitude
		if dist < minDist then
			minDist = dist
			minPlayer = player
		end
	end
	return minPlayer
end

Don’t put the vector magnitudes in a table. You need to calculate it on the fly right when you need it because players move, and the table data will get outdated very quickly. The npcModel variable is the character of the NPC. You have to use that since NPCs do not have a player associated with them.

Then to get to the player, use the pathfinding service. It will make the NPC follow a path to the player, even if the NPC cannot directly see the player.