How to create objects in a more optimized way?

Well, I have a question and it is how I can create things in an optimized way, since the normal script is very long like this:

local parte = Instance.new("Part")
parte.Parent = FolderParts
parte.CanCollide = false
parte.Transparency = 1

to something like this:

local Parts = {

OnePart = {Instance.new("Part", workspace),},

}

I know this is the base but there are parts in which I cannot put the = or a number

2 Likes

Using the Instance.new() parent argument isn’t the best way… Here’s why: PSA: Don't use Instance.new() with parent argument

1 Like

A shorter script doesn’t always mean better, you should checkout this post: PSA: Don't use Instance.new() with parent argument - #2 by zeuxcg

1 Like

You could create a function that assigns properties to your new part. Here is an example:

local function createObject(class, parent, properties)
    local obj = Instance.new(class)
    for i,v in pairs(properties) do
        obj[i] = v
    end
    obj.Parent = parent
    return obj
end

local Parts = {
    OnePart = createObject("Part",workspace,{CanCollide=false,Transparency=1})
}

EDIT: Forgot to add return obj. Pretty important.

7 Likes

instead of:

local part = Instance.new("Part")
parte.Parent = FolderParts
parte.CanCollide = false
parte.Transparency = 1

do

local part = Instance.new("Part")
part.CanCollide = false
part.Transparency = 1
part.Parent = FolderParts -- Parent is last

Since we’re changing the way the part appears, we want to do it first before parenting into workspace. That way, it doesn’t have to re-update in workspace to change its apperance.

(the posts that they linked above explain more but heres a general summary.)

Yours:
Create part
parent part to workspace
update visual on part – default
change visual on part again.

New:
Create part
update visual on part
parent part to workspace.

Or something along the lines of that :man_shrugging: (just try reading on the PSA post.)

1 Like

Make sure that if you are using this function, when you want to create an object without adding any custom properties, make sure to include an empty table. Example:

createObject("Part",game.ServerStorage,{})

EDIT: There are ways to avoid this really small inconvenience, like adding a check inside the function, but that does slow things down (very minorly).

1 Like

it gives me this error:: invalid argument #1 to 'pairs' (table expected, got Instance)

1 Like

Make sure when calling the createObject function, the first parameter is the string value of the class of the object you want to create, the second parameter is the Instance that the created object is parented to, and the third parameter is a table containing the properties that you want to add to the created object. The table must be in this format:

{PROPERTY = VALUE, PROPERTY2 = VALUE2}

Example using multiple properties:

createObject("Part",workspace,{Anchored=true, Transparency=0.5, Size=Vector3.new(5,5,5), Position=Vector3.new(10,0,0)}
1 Like

Oh, and another question to finish. How can I put a parent with another object created in it?

OnePart = createObject("Part", workspace,{CanCollide = false,Transparency = 1}),
OneWeld = createObject("Weld",{parent = OnePart, CanCollide = false,Transparency = 1}),

or

OnePart = createObject("Part", workspace,{CanCollide = false,Transparency = 1}),
OneWeld = createObject("Weld", OnePart,{CanCollide = false,Transparency = 1}),

You would use the second option. Make sure not to skip the second parameter, or else an error will occur.

I already tried but it doesn’t work

Ah, I know why. The part you are creating, since it’s CanCollide property is set to true, is falling to the bottom of the map and deleting. You should add the Anchored property and set it to true to stop it from falling and deleting.

I already set the anchored to true and now it gives me an error in

Should’ve explain this further. You need to add the Anchored property to the Part, not the Weld. Here is an example using your script:

OnePart = createObject("Part", workspace,{CanCollide = false,Transparency = 1,Anchored=true}),
OneWeld = createObject("Weld", OnePart,{}),

Also, Transparency and CanCollide aren’t properties of Weld

with this, now it gives me an error in the pairs part

Can you show the error? Maybe you mistyped something.

Says the error is occuring on the TwoWeld. I can’t see why it’s any different than OneWeld.

But, another issue that I noticed is that since they are both part of the same table, you can’t refer to other members of the table while still inside the table. I suggest separating the tables, or even making another function that makes both the part and the weld.

1 Like