Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
sad_eyez 162
8 years ago

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)
0
Pretty sure you can't call GetChildren on a table. GetChildren returns a table of the objects descendants. Meltdown81 309 — 8y
0
How do i get all the objects in each of the pages then? sad_eyez 162 — 8y

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

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)

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 Exanimatedn9dean 37 — 8y
0
there's a lot of other ways of doing this, it's just how i do it. Exanimatedn9dean 37 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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

Answer this question