I have a script that creates parts, however I want to change their properties at some point in time. Basically when I create a part I want to store the object so that I can do things with it.
How do I store instances create by Instance.new()?
2 answers
You would have to store them in a table, like this:
local parts = {}
local x = Instance.new("Part")
parts[#parts + 1] = x
function findPart(part)
if type(part) ~= "userdata" then return end
for i=1, #parts do
if parts[i] == part then
return parts[i]
end
end
end
print(findPart(x))
This would simply loop through the table and find the part you're looking for, as indexing the actual instance into the table wouldn't work.
Then you could simply do this:
local xPart = findPart(x)
xPart.Name = "FoundPart"
14 comments
um cant u use a array instead of a dictionary
This is an array, a dictionary is where you index the actual instance. Assuming you just got the two switched, I personally just dont like dictionaries, as two parts that potentially have the same name could have problems.
Technically, arrays and dictionaries are both the same thing in Lua. Also, you should work to get over your aversion to them, because they're a REALLY useful tool for certain situations. Edit: @below, yes they are, source -> https://www.lua.org/pil/2.5.html
Arrays and dictionaries are NOT the same thing in Lua.
Do some research before making claims that are incorrect.
The creation of arrays and dictionaries is so similar in Lua that it causes people to assume they are the same. However, they are actually different. Why do you think ipairs loops through "arrays" but not dictionaries?
http://www.luafaq.org/gotchas.html#T6 note the difference between arrays and dictionaries. Just because a table can be both, does not mean they are the same thing.
Doesn't answer the question
Does this mean that I can store the objects returned by Instance.new() in a table, then if I need to edit it later, just go in the table and edit that?
@Phlegethon, they're both associative arrays.
Your link literally says "Tables are both arrays and dictionaries"
https://www.lua.org/pil/2.5.html states quite clearly that they're both associative arrays.
@ Phlegethon5778, please do some research before trying to call people out on things.
Also, ipairs and pairs both work on any arbitrary table, regardless of whether you're calling it an array, a list or a dictionary. The reason they function differently is because they're different iterators with different implementations.
Simple? Use 'local'
local part = instance.New("Part") -- Creates a part
part.Parent = game.Workspace -- puts in workspace
part.Position = vector3.New(0,0,0) -- put the part to this position in workspace
Don't know if this is what you are looking for.. but oh well?
7 comments
For first, its local not Local and its not game.workspace, is game.Workspace or workspace
well i did this not in studio lmao
so negative lmao
changed it to suit ur preference lmao
game.workspace and Workspace (as a global variable) are deprecated, game.Workspace or workspace is fine. However this doesn't answer the question at all
Sorry this only adds properties to it, when I would try and change their property the scope would be lost
isnt that what u wanted? lmao.... look at ur questions.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
x = Instance.new("Part")?Or do you mean you want to access the part in some other script?