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".
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"