Question about {}s

Hey guys. I have a question about tables. Is it possible to get a specific value from a table without using a variable? I know that question is probably hard to understand, so here’s what I mean.

Using a variable:

local Apples = {RedApple = workspace.RedApple, GreenApple = workspace.GreenApple}
print(Apples.GreenApple)

Without using a variable:

local Apples = {}

for i, v in workspace:GetChildren() do
	if v:HasTag("Apple") then
		table.insert(Apples, v)
	end
end

print(Apples.GreenApple) -- Won't print because there isn't a variable named "GreenApple".
1 Like

I don’t understand what you mean exactly? Are you talking about this?

local fruits = {"Apple","Strawberry","Peach"}
print(fruits[1]) -- would print Apple.
1 Like

Yes, you can. Here is how I did it.

local Apples = {} -- We declare our table

local children = game.Workspace:GetChildren() -- we grab all the children in workspace

for i,v in children do -- Loop through children
    if v:HasTag("Apple") then -- check if they the tag "Apple"
        Apples[v.Name] = v -- If they do then add it to the table. If you named your part or object to "GreenApple" then it will have that as name.
    end
end

print(Apples) -- Print out the table
print(Apples.GreenApple) -- Print out "GreenApple"
1 Like

Omg I literally tried this, but I kept getting nil returned to me. (I was doing it wrong, that’s why :melting_face:.) Thank you so much dude!

1 Like

I suppose, yes.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.