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.
_ 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.
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
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)
(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
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.
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
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)
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