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

How would i use a table to create new parts?

I am learning about tables now and it seems like you have "functions" to do with it like table.find I would like to know more about it and how i can create parts with table

1 comment

0

https://developer.roblox.com/en-us/articles/Table This article has all the information you could ever need about tables.

2 answers

✓ Accepted answer

To create parts with a table is pretty easy, but you can do more interesting stuff with it as well.

For example, we can make a script that takes colors out of a table and makes parts that will be that same color.

ServerScript:

local PartColors = {"Really red", "Really blue", "New yeller", "Burnt sienna"}

for i,v in pairs(PartColors) do
	local Part = Instance.new("Part", workspace) -- We create a part in the workspace
	Part.Name = "Part"..i --This will name the part "Part1" and up to "Part4", since we have 4 colors, the loop will run 4 times.
	Part.BrickColor = BrickColor.new(v) --We can set the current part BrickColor to the color in the table. These colors are going to be set in the order that they are shown in the table
end

This is an easy little introduction that you can do with Tables and For Loops, but however, you will need to set the position of the part in the script yourself. If you want me to write that as well feel free to comment on this answer. Hope this helped you!

1 comment

0

Thank you so much! that really helped me understanding it!

This question seems vague so I'll try to explain. As we learn more Lua, we grow up with Instances, thus we are used to index a property and assign it a value. It is subtle but we have actually been working with Object-Oriented-Programming (OOP). Therefore the Instances we worked with are very similar to tables.

So if we assume that the Index of the table is it's property and the value is the value, we can use a for loop to assign values to an Instance.

Samples:

To use a table (dictionary in this case) to assign multiple values

local Part = Instance.new("Part")

local Dictionary = {
	Parent = workspace;
	Name = 'Wow potato';
	Transparency = .5;
	Size = Vector3.new(1,2,3)
}


for property,value in pairs(Dictionary)do
	Part[property] = value
end

A function which can change multiple values of tables and instances.

local Potato = function(t,p,w)
	for prop,val in pairs(p)do
		local success = pcall(function()
			t[prop] = val
		end)
		if w and not success then
			warn('error occured',t,prop,val)
		end
	end
end

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