I have a script that has a decent amount of children that I need to reference all of with variables, so I wanted to make it a little more efficient by having it automatically get all the children inside with a table and assign different variables to them. This is the script I have right now:
local cdn = script:GetChildren() for i = 1, #cdn do local child = cdn[i] anim(i) = child end
My issue is on line 4, it gives me an error saying "Expected identifier when parsing expression, got =", so what do I do to fix this?
Revised Solution:
So I didn't get your response from what you were trying to do earlier, so let me explain your error. You didn't define the variable before you did that for-i-loop, so here's a fix I did.
local anim local cdn = script:GetChildren() for i = 1, #cdn do local child = cdn[i] anim[i] = child end
Yo cannot create a variable out of thin air the way you wish to. Believe me I’ve tried several times before as a beginner. You however can achieve a similar effect to what you desire by creating a dictionary
type array. This table will allow you to reference a Child by their name as an Index to the list.
local function GetChildrenListFor(Parent) local ChildList = {} do for i,Child in pairs(Parent:GetChildren()) do ChildList[Child.Name..i] = Child end end return ChildList end local ChildList = GetChildrenListFor(script) print(ChildList["Anim1"])
Although I normally don’t question many different programs, you can simply just do script.ChildName
whenever you wish to use an Object.