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.
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}
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
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