Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What am i doing wrong with this search bar, It keeps giving me an error?

I am trying to make the search button find a specific name in all the pages

Error: 12:50:51.420 - Players.Player1.PlayerGui.Paid Winners GUI.Main.Main:26: attempt to call method 'GetChildren' (a nil value) 12:50:51.421 - Stack Begin 12:50:51.423 - Script 'Players.Player1.PlayerGui.Paid Winners GUI.Main.Main', Line 26 12:50:51.424 - Stack End

Code:

local allpages = {page1, page2, page3, page4, page5, page6, page7, page8, page9, page10}
local searchbox = script.Parent:WaitForChild("SearchText")
local searchbutton = searchbox:WaitForChild("Click")


searchbutton.MouseButton1Click:connect(function()
	for i,v in pairs(allpages:GetChildren()) do
		if v.Text == searchbox.Text then
			searchbox.Text = "Player has been paid!"
			searchbox.TextColor3 = Color3.fromRGB(0,200,0)
			wait(1.5)
			searchbox.Text = "Search..."
			searchbox.TextColor3 = Color3.fromRGB(95,95,95)
		end
	end
end)

2 comments

0

Pretty sure you can't call GetChildren on a table. GetChildren returns a table of the objects descendants.

0

How do i get all the objects in each of the pages then?

2 answers

✓ Accepted answer

This iterates through the table by indexing it with a number for example, allpages[1] would be equal to allpages.page1 since everything in a table has an index and a value. It's somewhat hard to explain , you'll understand it eventually if you work with tables alot.

local allpages = {page1, page2, page3, page4, page5, page6, page7, page8, page9, page10}
local searchbox = script.Parent:WaitForChild("SearchText")
local searchbutton = searchbox:WaitForChild("Click")


searchbutton.MouseButton1Click:connect(function()
    for i = 1, #allpages do
        if allpages[i].Text == searchbox.Text then
            searchbox.Text = "Player has been paid!"
            searchbox.TextColor3 = Color3.fromRGB(0,200,0)
            wait(1.5)
            searchbox.Text = "Search..."
            searchbox.TextColor3 = Color3.fromRGB(95,95,95)
        end
    end
end)

2 comments

0

for i = 1, #allpages would count from 1 to the number of instances inside the table allpages. allpages[i] would index each value from that

0

there's a lot of other ways of doing this, it's just how i do it.

Just as Exanimated... said, there are other ways of iterating through your table, I can think of three. To keep it as close to your code as possible, just get rid of the ":GetChildren" in the parenthesis for "pairs". This is because the "in pairs" method of iteration in a for loop accepts a table argument, so just give it the table.

Also, I would change "in pairs" to "in ipairs" because you don't have any String keys; it will not affect your code.

Here's a little more information on for loops and methods of iteration:

for index, value in ipairs(table) iterates through every element in the table with an index associated with it.

for i = 1, #table do From experience, this is functionally identical to "in ipairs" and does not iterate over elements with stringKeys as it literally only processes table[i].

for stringKeyOrIndex, value in pairs(table) iterates through every element in the table with its respective stringKey OR index.

Ex.


table = {}
table[1] = "blah" -- element at index 1
table.hello = "world" -- element at stringKey "hello"

for i, v in ipairs(table) do
	print("Index: "..i.."| Value: "..v)
end
-- Above will print: 
--	Index: 1| Value: blah

for i = 1, #table do
	print("Index: "..i.." Value: "..table[i])
end
-- Above will print: 
--	Index: 1| Value: blah

for k, v in pairs(table) do
	print("KeyOrIndex: "..k.." Value: "..v)
end
-- Above will print(Order might not be correct): 
--	KeyOrIndex: 1| Value: blah
--	KeyOrIndex: hello| Value: world

This discussion is preserved as it was. New answers, comments, and votes are closed.