Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use GetChildren to select and change multiple parts?

Asked by 7 years ago

I used GetChildren here to get these multiple fireball shots and slowly delete them. But it will always say transparency is nil. What am I doing wrong here? How can I fix it?

local fireballs = Character:GetChildren("FireBall")
repeat wait(.1)
fireballs.Transparency = fireballs.Transparency + .1
until fireballs.Transparency >= 1
0
The problem here is that get children returns a table of multiple objects, not just one object. Essentially, as crazycat said, you need to loop through the whole table with for i,v ipairs and change each ones transparency like that. User#17125 0 — 7y
0
D: stop being mean to your computer doing more comparisons instead of using a for loop hiimgoodpack 2009 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago

:GetChildren() returns a table (and takes no arguments, so you saying "FireBall" does nothing). In lua, you cannot apply an action to a table in order to apply that action to everything within it. That is, when you say fireballs.Transparency =, lua assumes you're trying to change the table rather than the list of children. To change the children, you have to use a for loop, like so:

local ch = Character:GetChildren()
local fireballs = {} -- This creates a new table. We will use it to keep track of the parts we're interested in (fireballs).
for i = 1, #ch do -- For each child:
    if ch[i].Name == "FireBall" then -- If this child (ch[i]) has the right name, add it to our list
        table.insert(fireballs, ch[i]) -- you can also do: fireballs[#fireballs + 1] = ch[i]
    end
end
if #fireballs > 0 then -- The "until" line will error if there aren't any fireballs
    repeat
        wait(0.1) -- wait once and then, for each fireball, increase its transparency
        for i = 1, #fireballs do
            fireballs[i].Transparency = fireballs[i].Transparency + .1
        end
    until fireballs[1].Transparency >= 1 -- Notice that 'i' doesn't exist here so we need to access one of the fireballs; we look at the first one to see when to stop
end
--Alternatively, you could use a for loop (but don't keep both):
for i = 0.1, 1, 0.1 do
    wait(0.1)
    for j = 1, #fireballs do
        fireballs[j].Transparency = i -- In this case, 'i' is the transparency and 'j' is which fireball we're working with
    end
end
0
You gave too much non-useful information. The final six lines with an explanation was all you needed. CootKitty 311 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago

For i,v in pairs instead?

0
use commemts for these kind of "answers"!!!! abnotaddable 920 — 7y
0
0
Sorry, still new here 6xxnoobxx6 -5 — 7y

Answer this question