Finding a specific object in the workspace quickly

I’m using a for loop to loop through the entire workspace to find surfaceGuis named “Prompt” but it takes a while as it has to loop through the entire workspace. Is there a way to just search for surface gui or something?

code:

while true do
	for i, prompt in pairs(game.Workspace:GetDescendants()) do
		if prompt.ClassName == "SurfaceGui" then
			if prompt.Name == "Prompt" then
				local shownPrompt = PromptModule.CheckShowPrompt(player, prompt)
			end
		end
		
		task.wait()
	end
end

Use :FindFirstChild(“Name”, true)

There are multiple prompts in the workspace, will that cause issues?

It searches for the name so probably not

The issue with using FindFirstChild for this is as the name suggests, it finds the first, and only the first child pertaining to the criteria given, in this case the name.

It may be worth your time to create a Folder in workspace and place the objects in there. Then you can loop through that folder rather than the entire workspace.

Additionally if you just wanted to search for SurfaceGuis, you could do if prompt:IsA(“SurfaceGui”), but I’m unsure if that would make the entire thing quicker as it looks like the reason it’s taking so long is because it’s searching the entire workspace. You could if you wanted to store all of the prompts in a table as well, and then loop through that table.

1 Like

It only works on one prompt when i need it to work on every one

Would you recommend making a table or a folder?

Honestly depends on the use case but for simplicity purposes I would probably just create a Folder in workspace and loop through that