0

# Get all the values of a table?

I am apparently a beginner with the table functions and I want to know if there is a method to get all the values of a table and change their properties. This is the script:

tablepart = {} -- my table
function parts()
local parts = script.Parent:GetChildren() -- creates a table with the children of the model
for i = 1, #parts do
if parts[i]:IsA('BasePart') then
table.insert(tablepart, parts[i]) -- create a new table with the children of the model that are parts
end
end

end

wait(7.5)
parts() -- call the function to test it


I know I can just insert the script below after the second 'end' but it would only change the transparency of the parts one by one...

for i = 1, #tablepart do
repeat
wait(1)
tablepart[i].Transparency = tablepart[i].Transparency + 0.1
until
tablepart[i].Transparency == 0.5
end


0
Your script does not work because a For loop runs the first time for the first number, then a second time for a second number, etc. That means, your script takes the first part, changes its Transparency until it's 0.5, and just then changes to the next part. TheArmoredReaper 173 — 9y

0
BlueTaslem 18071
9 years ago

There isn't exactly. The simple way to fix your set up is to lift the wait out:

repeat
wait(1)
for i = 1, #tablepart do
tablepart[i].Transparency = tablepart[i].Transparency + 0.1
end
until tablepart[1].Transparency >= 0.5


We actually can make a way to "do something to every value" -- this is called map in most languages. Lua doesn't implement it by default, but it's very brief to do yourself:

function map(fun, tab)
for i,v in pairs(tab) do
tab[i] = map(v)
end
end


The tab[i] = part is not really useful in this case, but I'll keep it.

We must now write a function which acts on a single part:

function trans(part)
part.Transparency = part.Transparency + 0.1
return part -- to make map happy
end


Now we just map our trans function 5 times:

for i = 1, 5 do
map(trans, tablepart)
end