Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

1

x = Instance.new("Part") ?

0

Or do you mean you want to access the part in some other script?

2 answers

✓ Accepted answer

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

0

um cant u use a array instead of a dictionary

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.

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

0

Arrays and dictionaries are NOT the same thing in Lua.

0

Do some research before making claims that are incorrect.

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?

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.

0

Doesn't answer the question

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?

0

@Phlegethon, they're both associative arrays.

0

Your link literally says "Tables are both arrays and dictionaries"

0

https://www.lua.org/pil/2.5.html states quite clearly that they're both associative arrays.

0

@ Phlegethon5778, please do some research before trying to call people out on things.

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.

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

0

For first, its local not Local and its not game.workspace, is game.Workspace or workspace

0

well i did this not in studio lmao

0

so negative lmao

0

changed it to suit ur preference lmao

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

0

Sorry this only adds properties to it, when I would try and change their property the scope would be lost

0

isnt that what u wanted? lmao.... look at ur questions.

This discussion is preserved as it was. New answers, comments, and votes are closed.