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

How do I store instances create by Instance.new()?

Asked by 5 years ago

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.

1
`x = Instance.new("Part")` ? fredfishy 833 — 5y
0
Or do you mean you want to access the part in some other script? fredfishy 833 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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"
0
um cant u use a array instead of a dictionary User#23365 30 — 5y
0
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. ObscureIllusion 352 — 5y
0
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 fredfishy 833 — 5y
0
Arrays and dictionaries are NOT the same thing in Lua. User#25115 0 — 5y
View all comments (10 more)
0
Do some research before making claims that are incorrect. User#25115 0 — 5y
0
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? User#25115 0 — 5y
0
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. User#25115 0 — 5y
0
Doesn't answer the question User#24403 69 — 5y
0
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? Formidable_Beast 197 — 5y
0
@Phlegethon, they're both associative arrays. fredfishy 833 — 5y
0
Your link literally says "Tables are both arrays and dictionaries" fredfishy 833 — 5y
0
https://www.lua.org/pil/2.5.html states quite clearly that they're both associative arrays. fredfishy 833 — 5y
0
@ Phlegethon5778, please do some research before trying to call people out on things. fredfishy 833 — 5y
0
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. fredfishy 833 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago
Edited 5 years ago

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?

0
For first, its local not Local and its not game.workspace, is game.Workspace or workspace yHasteeD 1819 — 5y
0
well i did this not in studio lmao XviperIink 428 — 5y
0
so negative lmao XviperIink 428 — 5y
0
changed it to suit ur preference lmao XviperIink 428 — 5y
View all comments (3 more)
0
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 User#24403 69 — 5y
0
Sorry this only adds properties to it, when I would try and change their property the scope would be lost Formidable_Beast 197 — 5y
0
isnt that what u wanted? lmao.... look at ur questions. XviperIink 428 — 5y

Answer this question