Help With Selection:Set()

My Issue: Lacking Permission
My Goal: Make a Plugin
My Script Type: Local Script
My Code:

local Selection = game:GetService("Selection")
script.Parent.FocusLost:Connect(function(Enter)
	if Enter then
		local FoundTable = {}
		for i, _ in pairs(game:GetDescendants()) do
			if string.match(_.Name:lower(), script.Parent.Text:lower()) then
				table.insert(FoundTable, _)
			end
		end
		Selection:Set(FoundTable)
	end
end)

What are you trying to do? The Selection Service is meant for plugins.

I am trying to make a plugin but I am using a Gui Textbox so I can search for items to select. For eg: I will search “Baseplate” in the textbox and the textbox will select every item in the game named “Baseplate”

My apologies, but it does say his goal is to create a plugin?

Oh my bad I didn’t see the “goal: make a plugin” thing at the top :sweat_smile:

Yes, that is the goal I have. I want to create a search plugin that selects every item you searched for

Do you have any idea on how to make it work?

Can’t you use a remote event and set it on the server?
I’m not too experienced with plugins though.

Please discard what I just said, I do not want to get off track of reaching my main goal

You probably get the lacking permission error because your code cycles through every instance in the game, which also includes core scripts in the CoreGui, which you’re not allowed to tamper with. If you really must use :GetDescendants(), wrap it in a pcall to avoid errors.

local Selection = game:GetService("Selection")
script.Parent.FocusLost:Connect(function(Enter)
	if Enter then
		local FoundTable = {}
		for i, _ in pairs(game:GetDescendants()) do
			pcall(function() 
				if string.match(_.Name:lower(), script.Parent.Text:lower()) then
					table.insert(FoundTable, _)
				end
			end)
		end
		Selection:Set(FoundTable)
	end
end)
1 Like

I think the main reason is I have to use it in a plugin, which means I cannot select things in the game via script. I will keep your code inside my script and try it out when it is a plugin and see what I get.(When I ran the code while it was a plugin, it worked)