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

How do you get the number from a :GetChildren()?

Asked by 4 years ago

Using a

local chars = items:GetChildren()
for i = 1, #chars do

How would I make it create a new value with the name being the number of the "i"?

1 answer

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

You could use the _G function to accomplish that.

local chars = items:GetChildren()
for i,v in pairs(chars) do
    _G["Item" .. i] = "foobar"
end

I also changed the for loop so it's a bit better (i would say). You can use v to reference the object that has been indexed by the i value, basically the object is chars[i].

You can then use this little bit of code to reference that value.

print(_G["Item" .. 1])

output:

foobar

The _G function is global so all scripts can access it.

1
Thank you for answering and going into detail on what everything means! THELASTPlCKLE 57 — 4y
0
this is big bad dont do Elixcore 1337 — 4y
0
`_G` isn't a function, its a table. EpicMetatableMoment 1444 — 4y
Ad

Answer this question