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

Isn't letting me asign variables to the children inside the script?

Asked by 4 years ago
Edited 4 years ago

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?

2
I believe you used parentheses instead of square brackets; this will cause the compiler to expect a function identifier. Ziffixture 6913 — 4y
0
I tried that and now I get an error for the anim part saying "attempt to index global 'anim' (a nil value)" EnderGamer358 79 — 4y
0
Have you defined the variable, "anim"? AnExactAltOfNampha 23 — 4y
0
No, I'm wanting "anim" to reference the different children inside the script, with anim1 corresponding to the first child, or anim7 corresponding to the seventh child, and so on. EnderGamer358 79 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
0
This is close to what I was wanting but not quite, I'm wanting the for loop to create a bunch of variables, each one referencing a different child on that table, hence the (i) at the end of that anim variable. So anim1 would be the first child, anim7 would be the seventh one, etc. I'm just trying to figure out what I would have to change to accomplish this. EnderGamer358 79 — 4y
0
I get what you are trying to do now. I have edited the post. AnExactAltOfNampha 23 — 4y
0
Aaaaa, this looks good but it's giving me an error saying "attempt to index local 'anim' (a nil value)" referencing Line 6 in this case. I'm executing this from a regular script if that helps anything. EnderGamer358 79 — 4y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

0
I like this solution! I'm not feeling in the mood of ideas and didn't come up with using arrays. AnExactAltOfNampha 23 — 4y

Answer this question