Script timeout: exhausted allowed execution time on FindFirstAncestorOfClass when server starts up?

I am looking for some insight on my issue that crashes the whole game. I have experienced this before while developing, however since then I improved my code and I thought this issue was resolved, however today I looked at an “Error Report” for my game and I saw this error again:
“Script timeout: exhausted allowed execution time”

This is my ModuleScript that I require in a Script that starts up with server:

local CollectionService = game:GetService("CollectionService")

local LightsServer = {}

local function getBaseModel(instance)
	local itemsFolder = instance:FindFirstAncestorOfClass("Folder") -- This is the line the error is pointing to
	if not itemsFolder then
		return
	end

	local baseModel = itemsFolder.Parent
	local ownerName = baseModel:GetAttribute("Owner")
	if not ownerName then
		return
	end
	
	return baseModel
end

CollectionService:GetInstanceAddedSignal("Light"):Connect(function(instance)
	local baseModel = getBaseModel(instance)
	if not baseModel then
		return
	end
	
	local modelLightInstances = LightsServer.LightInstances[baseModel]
	if not modelLightInstances then
		modelLightInstances = {}
		LightsServer.LightInstances[baseModel] = modelLightInstances
	end
	
	modelLightInstances[instance] = true
	
	instance.AncestryChanged:Connect(function(child, parent)
		if not parent then
			modelLightInstances [instance] = nil
		end
	end)
	
	local ownerName = baseModel:GetAttribute("Owner")
	local owner = Players:FindFirstChild(ownerName)
	if owner then
		local success, playerSettings = _G.DataServer:GetData(owner, "Settings"):await()
		if success then
			instance.Enabled = playerSettings.Lights
		end
	end
end)

return LightsServer

My suspicions are that this problem is caused because there are like 10000+ instances with the tag “Light” in the ServerStorage, however this might also be a red herring, because they are already tagged and the signal should not fire. Also, the line the error is pointing to shouldn’t take that long to execute to cause this error either, so I would love to hear more opinions. :thinking:

Can you show where you use/require the script, with its initial code

local function initComponents()
	print("Loading Components")
	local componentsScripts = ServerScriptService.ServerComponents:GetChildren()
	for _, componentScript in ipairs(componentsScripts) do
		MainServer.Components[componentScript.Name] = require(componentScript)
		task.wait(0.1)
	end
end

do
	print("Loading", script.Name)
	initComponents()
end

Normally the error means a loop is running too fast and/or its too time consuming. I don’t really see the issue in here.

1 Like

I am experiencing this as well. Old code that worked fine has suddenly been timing out. I will get logs for it in a bit but this only started happening within the past 30 days. Nothing was changed in the code. I am not sure if your code is this issue or perhaps something was changed without us knowing