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

How to grab all children from a group and un-anchor them?

Asked by 7 years ago

I tried to use the "GetChildren" function thing, but it didn't work. Here is my script:

local children = script.Parent.Parent.Pellets:GetChildren()
wait(0.4)
script.Parent.Transparency=0
script.Parent.Warning:Play()
wait(1.7)
script.Parent.Attack:Play()
children.Anchored = false
script.Parent.Transparency=1
wait(3.5)
script.Parent.Parent:Destroy()

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

To accomplish this, you're going to need to use a for loop. :P

Just a heads up: I'm not going to be re-writing (editing) your code, b/c it would feel like I did all the work for you; if someone did that w/ me, I wouldn't be happy... Buuuut also b/c it's super simple (once you get the hang of it). :P

First, lets set up a(n example) table:

local TableExample = { -- 'TableExample' is the name of the table; it'll be used later on; a table hold data, and the data can be retrieved from it
    'Value1', -- Do I need explain these? :P
    Value2 = true,
    [tostring('Value' .. 3)] = true,
    'Value4'
}

Now that we got our table set up, lets begin the process (of explanations!) :D Onto... the magical for loops!! :O For this example, I'll be using a generic for loop:

for i, v in pairs(TableExample) do -- I forgot what the 'i' represents; however, I do know that 'v' represents the value at the current position of the table
    print(i, v) -- Prints what 'i' & 'v' are
end -- Ends code/ chunk/ .....Whatever XD

And, ta-da! There's our for loop! :D Now, when we fire the above code(s(?), the output will return the following:

--[[
    nil Value1
    Value2 true
    Value3 true
    nil Value4
--]]

Aaaaaaand wa-la! That's (an example of) how you use the generic for loop! :D

What the generic for does is returns the data(s) from the table, depending on how many items/ storage's there're. (Say you have 3 items in the table: the generic for, w/ pairs (or if you prefer, next)). (Ugh, I feel that that didn't explain it at all/ didn't represent the functions properly. ;-;) I forgot what the first value stands for, like in the generic for presented earlier w/ 'i, v', I do, however, know that the second value stands for the value at the current iteration (the generic for returns the value, and same w/ the first value as well).

Stuff touched on, but didn't go into great detail about

  1. Tables - Stores data such as strings, numbers, characters, objects (children), and/ or information.

  2. For Loops - There's a lot to cover, so if you wish to look up on it more, click on the highlighted For Loop sentence-starter before the explanation even begins. :)

  3. The Generic For - Already explained, so this is optional. :P However, if you wish to research more, you can, like the last, click on the highlighted words. :)

  4. Pairs Iterator - This is a tough one to explain, so please read the wiki document. ':)

  5. Next Iteration - ^

Hope this helped you in any way! :D

Ad

Answer this question