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

How would i use a table to create new parts? [closed]

Asked by 4 years ago

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

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

Closed as Not Constructive by DeceptiveCaster

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
0
Answered by
Ghost40Z 118
4 years ago

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!

0
Thank you so much! that really helped me understanding it! tomer495 19 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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