How to set multiple "Instance.New" parameters

Hey,

I’m just wondering if it is possible to set more than 1 parameters on an “Instance.New” function.
I know that the first parameter is the parent, but it is possible to add more parameters, like the name and the value ?

It is mostly to save some lines of codes on my scripts.

Here is my current script:
Capture 1

Here is an example of what i want to do
Capture 2

1 Like

Maybe you can use like a for loop to make them, but that’ll just increase the number of lines. And tbh I don’t really think the extra lines of leaderstats will affect your script ( As the leaderstats should be kept in a separate script?? )

You can not do that with the regular Instance.new, however you can use this function in order to achieve the wanted results

local function Create(Type)
    return function(Parameters)
        local obj = Instance.new(Type)
        for Parameter, Value in pairs(Parameters) do
            if type(Parameter) == 'number' then
                Value.Parent = obj
            else
                obj[Parameter] = Value
            end
        end
        return obj
    end
end

Here is an example

local Part = Create("Part"){Name = "Party Part Part", Parent = workspace, Anchored = true}
1 Like

I don’t really get why somebody needs to do this, the regular one is fine.

You can’t set any parameters other than the parent, and even setting the parent is highly discouraged. That’s because when you set the parent right off the bat, you haven’t configured the object YET, and therefore do not want it to already be in the game world. I suggest not specifiying any parameters other than the class and doing everything by hand.

Shorthand creation syntax can be really handy; the native constructor/property assignment syntax can get really annoying when you’re working with a lot of instances.

That’s why cleaner syntax like make is preferred sometimes

Instance.new(class, parent) is highly discouraged because it is actually slower than setting the parent with .Parent after configuring the part

1 Like

That doesn’t mean it isn’t cleaner… :wink:

I remember seeing a post about Instance.new and it said you could set multiple properties by doing something like this:

Example
local part = Instance.new("Part",{Parent = workspace})

it was a really old post though, so I’m not sure if it still works.

Just because something is subjectively cleaner doesn’t mean it’s objectively better, and using the parent argument of Instance.new is objectively, provably slower.

FYI if you could create a util function that parents last:


	function Util.Create(instanceType)
		return function(data)
			local obj = Instance.new(instanceType)
			local parent = nil
			for k, v in pairs(data) do
				if type(k) == 'number' then
					v.Parent = obj
				elseif k == 'Parent' then
					parent = v
				else
					obj[k] = v
				end
			end
			if parent then
				obj.Parent = parent
			end
			return obj
		end
	end

(From Set parent last for instances created with Utility.Create by DarraghGriffin · Pull Request #615 · Roblox/Core-Scripts · GitHub)

1 Like