generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Define new variables in a script

Previous Thread :: Next Thread 
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 03:29 AM
Hello! I was wondering if it is possible in the LUA language to CREATE a NEW variable from within the script as needed, ill keep this short.

Normally I pass 3 models to this script inside a table, however I may pass 1 or 5 models inside the table, my current solution is to predefine some variables and then use them for each model I go through. The problem is that I can never pass more than 5 models or else I crash and need to add more preset variables and more "elseif" statements.

-=-

Table1 = {}
Table2 = {}
Table3 = {}
Table4 = {}
Table5 = {}

function .Start(Models)

local M = Models
for m = 1, #M do
if m == 1 then
table.insert(Table1, m, M[m])
elseif m == 2 then
table.insert(Table2, m, M[m])
elseif m == 3 then
table.insert(Table3, m, M[m])
elseif m == 4 then
table.insert(Table4, m, M[m])
elseif m == 5 then
table.insert(Table5, m, M[m])
elseif
end
end
end

-=-

Bit cumbersome to have to do things like that, but if it has to be done that way then it has to be done that way.




tl;dr I need to define variables in this sense, similar to how you can concatenate strings (yes I know this is terribly incorrect)

for i = 1, 5 do
"Variable" .. i = i + i
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jan 2017 04:02 AM
Put all the tables in a table and do
table.insert(tbl["Table" .. m], blah blah
Report Abuse
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 04:14 AM
Thank you cntkillme, it doesn't exactly solve the problem at hand but it SERIOUSLY cuts down on spaghetti and length of the script.

What i'm looking for is to define the tables without having to create them at the beginning, so if I have 3 models the script creates 3 tables, if I have 5 models it creates 5 tables etc.

You have inspired me though, I wonder if this is doable by using a Dictionary.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jan 2017 04:20 AM
Yeah I kinda implied you should be using a dictionary. Anyways here's a snippet that might help and you don't have to use a dictionary in this example:

local Tables = { }
for _, v in pairs(whatever:GetChildren())
if v:IsA("Model") then
table.insert(Tables, { }) -- push a new empty table
end
end

And with that whole if/elseif chain:
table.insert(Tables[m], m, M[m])
Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
25 Jan 2017 05:23 AM
cntkillme's way is the best way, but if you'd like a working version of your pseudocode at the bottom, here is how you can do it:

for i = 1, 5 do
getfenv(0)["Variable"] .. i = i + i
end

the getfenv function with 0 as its argument returns a table that represents all global (not local) variables in the script


Report Abuse
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 05:32 AM
That works great, however i'm having a new problem. Which is why I wanted to define values by script. I cannot access the values nested in the tables. I can read how many values are in the main table but I cannot access the nested ones.

fo this example i have

Workspace
-Script
-ModelParent
--Model1
---Part1
---Part2
---Part3
--Model2
---Part4
---Part5
---Part6

and the script

local Tables = { }

for _, v in pairs(workspace.ModelParent:GetChildren()) do
if v:IsA("Model") then
table.insert(Tables, "Model.Name") -- push a new empty table
end
end

print(#Tables)

M = workspace.ModelParent:GetChildren() -- Gets the two models "Model1" and "Model2"
for m = 1, #M do
local sM## #[##################- Gets the three parts in the model
for sm = 1, #s###o -- for each part
table.insert(Tables[sm], sm, sM[sm]) -- ERROR
end
end


The Error I recieve is "Workspace.Script:16: bad argument #1 to 'insert' (table expected, got nil)" I've tried multiple ways of changing the way I ask for "Tables[sm]" but no luck for me.
Report Abuse
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 05:36 AM
Not sure why that got filtered.
Also I forgot to change back a value from while I was testing.



local Tables = { }

for _, v in pairs(workspace.ModelParent:GetChildren()) do
if v:IsA("Model") then
table.insert(Tables, { }) -- push a new empty table
end
end

print(#Tables)

M = workspace.ModelParent:GetChildren() -- Gets the two models "Model1" and "Model2"
for m = 1, #M do
local noFilter = M[m]:GetChildren() -- Gets the three parts in the model
for nf = 1, #noFilter do -- for each part
table.insert(Tables[nf], nf, noFilter[nf])
end
end

Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
25 Jan 2017 05:38 AM
>>> table.insert(Tables, "Model.Name") -- push a new empty table

you aren't inserting a table, you're inserting a string
and that string isn't the model's name, it's literally the string "Model.Name"
that might be a part of your problem


Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
25 Jan 2017 05:38 AM
nvm


Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
25 Jan 2017 05:46 AM
I couldn't figure it out, so based on what I assume you're trying to do with your script, I made a new one for you:

local Tables = {}
for _,v in next,game.Workspace.ModelParent:GetChildren() do
for i,vv in next,v:GetChildren() do
Tables[i] = vv
end
end

you can test it after with this:

print(#Tables) --print length of table for debugging
table.foreach(Tables,print) --print contents of table for debugging


Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
25 Jan 2017 05:48 AM
actually you want them in tables so here's my revised version:

local Tables = {}
for _,v in next,game.Workspace.ModelParent:GetChildren() do
for i,vv in next,v:GetChildren() do
Tables[i] = {vv}
end
end


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jan 2017 06:05 AM
shouldn't it be Tables[m] not Tables[nf] op?
Report Abuse
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 11:22 AM
It Should be Tables[m], I can see the example script gets confusing there because of all the GetChildren functions

Here's the script with notes since its come so far. And it works in my studio locally just to PRINT things.

-=-

Workspace
-Script
-ModelParent
--Model1
---Part1
---Part2
---Part3
--Model2
---Part4
---Part5
---Part6

-=-

local Tables = { } -- Master Table

for _, v in pairs(workspace.ModelParent:GetChildren()) do -- Get the list of models
if v:IsA("Model") then -- Make sure it is a model
table.insert(Tables, { }) -- Insert a new table into the master table
end
end

print(#Tables) -- Should be 2 if using the Hierarchy posted above (this will be 2)
-- if the bottom half works this will still be two since this counts OBJECTS so 2 tables with three
-- things in them is 2 tables

print(#Tables[1]) -- Should be 0, haven't added anything yet

M = workspace.ModelParent:GetChildren() -- Gets the two models "Model1" and "Model2"
for m = 1, #M do -- Accesses "Model1" and "Model2" individually, This is where the debugging
--example part stops, normally the function just recieves a table with models inside of the
--one table as an argument
local noFilter = M[m]:GetChildren() -- Gets the three parts in the model
for nf = 1, #noFilter do -- This goes through each part in the model
table.insert(Tables[m], nf, noFilter[nf])
-- m is for each model, each model gets a table, so Tables[m] means table1 for model1 and part1,2,3
-- nf is for parts, parts go in the table that corresponds to their parent
end
end

print(#Tables[1]) -- if script works this should be 3, since 3 parts in the first table
Report Abuse
HunterCop224 is not online. HunterCop224
Joined: 17 Aug 2009
Total Posts: 112
25 Jan 2017 11:53 AM
And that appears to work in testing! this is the final test script I used. Sorry to put it in model form, but the model includes the Example directory and script with all notes so that its nice and easy for anyone else with this problem to see.

https://www.roblox.com/library/627299739/Nested-Tables

I needed an icon i'd notice ...

-=-


local Tables = { } -- Master Table

for _, v in pairs(workspace.ModelParent:GetChildren()) do -- Get the list of models
if v:IsA("Model") then -- Make sure it is a model
table.insert(Tables, { }) -- Insert a new table into the master table
end
end

M = workspace.ModelParent:GetChildren() -- Gets the two models "Model1" and "Model2"
for m = 1, #M do -- Accesses "Model1" and "Model2" individually, This is where the debugging
--example part stops, normally the function just recieves a table with models inside of the
--one table as an argument
local noFilter = M[m]:GetChildren() -- Gets the three parts in the model
for nf = 1, #noFilter do -- This goes through each part in the model
table.insert(Tables[m], nf, noFilter[nf])
-- m is for each model, each model gets a table, so Tables[m] means table1 for model1 and part1,2,3
-- nf is for parts, parts go in the table that corresponds to their parent
end
end

print("Table[1] has " .. #Tables[1] .. " objects") -- if script works this should be 3, since 3 parts in the first table
print("Table[2] has " .. #Tables[2] .. " objects") -- ditto

for t = 1, #Tables do
for p = 1, #Tables[t] do
print(Tables[t][p].Name)
Tables[t][p].BrickColor = BrickColor.new("Institutional white")
end
end
Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
26 Jan 2017 06:36 AM
sorry for bumping this but @cntkillme but I have to talk to you about something & you make yourself super impossible to contact


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Jan 2017 09:26 AM
Anyone can message me just pm me?
Report Abuse
Informable is not online. Informable
Joined: 10 Aug 2016
Total Posts: 1778
26 Jan 2017 10:04 AM
actually for me your DMs are disabled


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
26 Jan 2017 11:18 AM
Weird. Check your privacy settings I guess.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image