Any way to search for an object that's inside a table?

Hello my name is sandessat(again) and I want to ask if there is any way to search for an object inside a table?

This is what I tryed to do:

local aboutMeTable = {}
aboutMeTable = script.Parent.aboutMeButton:GetChildren()

print(aboutMeTable["buttonClickEvent"].Text)
4 Likes
local aboutMeTable = script.Parent.aboutMeButton
for _,v in pairs(aboutMeTable:GetDescendants()) do
	print(v)
4 Likes

Can you explain me what are you doing with that code???

EDIT: And for what is _ in the for???

1 Like

Sure so the var aboutmetable is the button then we do _,v in pairs(aboutMeTable:GetDescendants()) this will get all the descendants under that have their parent as aboutMeTable.

To your other question _,v is a way of doing i,v but it lets you have more variables.

4 Likes

Ok okay, thank you!!

TAGS: Heart button is not working…

1 Like

So lets say in the button you have

button
text
fkjdlaslkfdsj

your final print will be;
button text fkjdlaslkfdsj

1 Like

_ in the for loop is just a replacement for the index value. When using both index and value, a lot of programmers replace it with _ as a means to leave it out of the loop. This is because we rely on the value, not the index, at least 99.9% of the time.

3 Likes

Is this good?

local aboutMeTable = script.Parent.aboutMeButton
for _,v in pairs(aboutMeTable:GetDescendants()) do
	print(v["buttonClickEvent"].Text)
end
1 Like

Yes and no if you are trying to just print the text of a specif text button then you would do this.

local aboutMeTable = script.Parent.aboutMeButton
for _,v in pairs(aboutMeTable:GetDescendants()) do
if v.Name == "buttonClickEvent" then
print(v.Text)
end
end
2 Likes

Oh okay, thanks.

TAGS: Heart button is not working.

1 Like

Also, what does in pairs do? ._. I’m so dunno.

1 Like

Another alternative, if you were just trying to find one object value in a non “nested” table is to use Table.Find

(Edit: Dont use strings to find objects in a table using this, Read below)

local  Table =    script.Parent.aboutMeButton:GetDescendants()
local Value = ---value

local Object =  Table[table.find(Table, Value)]

Edit:

@Sandessat
Note only use this (in your case) when you are looking for a instance(object value) in table or you are using get children (or it gets more complicated using this method). This method only(best) works for instances , if you actually give it an instance like this:

local value = script.Parent.aboutMeButton["buttonClickEvent"]

if you wanted a more intuitive (most likely better) way of searching for a object inside a table use a for loop, like mentioned in the rest of this topic:

function GetObject(Value, Array) --- a quick function

for _, Val in pairs(Array) do -- for each index in the array given, do

  if Val == Value.Name then ----- if we found the value return it

       return Val -- return the object/value
       end
   end
end

local Object = GetObject("buttonClickEvent", aboutMeTable)

Lua Table API reference : table | Documentation - Roblox Creator Hub
Loops: Documentation - Roblox Creator Hub

(as always there, might be better ways of going about this , but things mentioned in this thread/topic (so far) are really, productive methods. after messing around with tables and loops for about an hour, lol

if you have any questions feel free to ask

Have a great Day/Night!

12 Likes

Oh, thank you, this way is more easier!!!

1 Like

While yes this works if you are to look for more then one var at a time then you would use the for loop.
Also Tables as of late have been unreliable, for loops are much better, and have much more uses then the table method.

2 Likes

pairs is one of the iterator functions. You can read up more about stateless iterators here.

2 Likes

i pretty much agree. i personally tend to use loops more often then table.find or table.foreach because i find them more “powerful” and have more "customizability " to them , especially when i am doing more complex stuff, they are my go to option

2 Likes

A time where a table.find wouldnt work is for a function like this where the var you are looking for isnt already preset.
image

2 Likes

yep, that’s why i think loops (for loops) are more powerful in the long run, but sometimes, like the issue in this tread it might be easier just to use something like table.find to find a specific object( or instances) in a table, given that the instance is already defined as an instance

i played around with table.find and i found (no pun intended) that its kind of weird using strings in it( at least in this case for finding objects), i found that it works OK if you are using an instance (but preferable in some cases using loops, might be to benefit)

1 Like

Use a key. Keys are very handy in indexing tables. For example

MyTable = {Key1 = “Hello,”, Key2 = " world."}
print(MyTable.Key1…MyTable.Key2)

1 Like

the _ is used when you are not using the index, for example here I want to make it so only specific players will get a tool for example, (or any other action, just for defined player ids…)
If we use a ServerScript in ScriptService

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function()
	--so each time a player is added, this script will run to check whether..

   local guys = Players:GetPlayers()
      --define an array of players
   local DeveloperIds = {
451391631;
123123123;
124124124;--Player ids
}

  for _,guy in pairs(guys) do --now we iterate , using no index through the array of players , naming the index _ we can name it what we want but since we aren't using any here, we can call it _ , usually people use i or k
--we could call it _,loopedplayer , we define each value as what we want, 
     	if guy ~= nil then --if he is existant
    if not table.find(DevelopersIds,guy.UserId) then 
return 
        end--You're Id isn't mentioned , so we will end it here(there was no matching value in the table as this guy's id)
	       print(guy.Name.."has joined the server, and is a dev")
          --some function for that "guy" whose id matches like
    local tool = game.ServerStorage.Tool:Clone()
 tool.Parent = guy.Backpack
1 Like