How To Make Search Result Clickable + Change Players Location?

1. What I want to achieve:
A UI with a Search Box in Runtime. Enter your search query. Output a list with found script objects.
Make the search results “Clickable”. When clicked, the player position changes to the parent location of the script object. In this example, the location of the flag.

Step #1:
Enter your Search Query

Step #2:
Click Search

Step #3:
Print the result list (output)

Step #4:
Make the Search Result Clickable

Step #5:
When “Clicked”, change your players location to the position of the scripts parent location. In this example, the position of the flag.

  1. What is the issue?
    I am stuck at Step #4 with the coding. I can’t make the search result clickable. And once clicked, I can’t change the player position to the script’s parent location.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I already looked for solutions in the Developer hub, but couldn’t find any answer.

I was able to create a script that outputs the search result. However, I can’t:

  • I can’t make the search result clickable.
  • I can’t change the players location when clicking the search result.
local UpdateScore = game:GetService("ReplicatedStorage").RemoteEvent
local workSpace = game:GetService("Workspace")
local players = game:GetService("Players")
local resultTextBox = script.Parent.resultText
resultTextBox.ClearTextOnFocus = false
local queryTextBox = script.Parent.queryText
local num = 0
local textButton = script.Parent.Search

-- Function to clean up the name and make it valid for button naming
local function CleanButtonName(name)
	-- Replace any invalid characters with an underscore
	return name:gsub("[^%w_]", "_")
end

textButton.MouseButton1Click:Connect(function()
	local query = queryTextBox.Text
	resultTextBox.Text = query
	local allElements = workSpace:GetDescendants()
	local scriptObjects = ""
	local query = queryTextBox.Text:lower() -- Convert query to lowercase

	for _, v in pairs(allElements) do
		-- Check if the element is a descendant of the Script class
		if v:IsA("Script") and query ~= "" then
			-- Convert the element's name to lowercase for case-insensitive comparison
			if string.find(v.Name:lower(), query) ~= nil then
				local fullPath = v:GetFullName()
				scriptObjects = scriptObjects .. v.Name .. " Location: " .. fullPath .. "\n"
			end
		end
	end

	-- Clear existing results
	resultTextBox.Text = ""

	-- Create clickable TextButton GUI elements for each result
	local lineNum = 0
	for name, fullPath in scriptObjects:gmatch("([^\n]*)\n") do
		lineNum = lineNum + 1
		local textButton = Instance.new("TextButton")
		textButton.Name = CleanButtonName(name) -- Set the name of the button after cleaning it
		textButton.Parent = resultTextBox
		textButton.BackgroundColor3 = Color3.new(1, 1, 1)
		textButton.BorderSizePixel = 0
		textButton.Size = UDim2.new(1, 0, 0, 20)
		textButton.Text = name .. " Location: " .. fullPath
		textButton.TextColor3 = Color3.new(0, 0, 0)
		textButton.TextSize = 14

		-- Add a click event to each result button
		textButton.MouseButton1Click:Connect(function()
			-- Do something when the result is clicked
			print("Clicked on: " .. fullPath)
		end)
	end
end)

Thanks for any little help, appreciate it!

Download: .rbxl place file
My_Search.rbxl (958.6 KB)

1 Like

Hey, are you able to send a screenshot of your hierarchy?

1 Like

You can simply use TextButtons instead of frames for the results and inside them the script would add an objectValue corresponding to the object and then use a for loop to check if a button is clicked, and if it is it would change the local player’s humanoidrootpart cframe to the cframe of the object

1 Like

@Aqua_Turneur Thank you so much. I tried Buttons… but was also stuck… couldn’t get it done. I wanted to make sure that the Button Name is the name of the Script that was found.

here is a Screenshot, and you can also download the .rbxl place file, that contains the entire script.

Thank you so much for any little help,
appreciate it!

I’m sorry I don’t really understand what’s the problem

@Aqua_Turneur I don’t clearly understand the code. I use mainly chatgpt, but it only brings me to this point. To summarize the problem: The Script doesn’t work and doesn’t do what I want to do.

Ohh that’s why it looked so strange, I don’t recommend you to use chat gpt for long and complicated scripts maybe if you send him the script he might find the problem

Did my additional info help with this matter?