Instance Module

In response to this thread, I’ve put together a module that is essentially the “Create” module from RbxUtility,

return {
    new = function(className, secondParameter)
        local instance = Instance.new(className)

        if type(secondParameter) == "table" then
            if instance:IsA("BasePart") and secondParameter["FormFactor"] then
                instance.FormFactor = secondParameter["FormFactor"]
            end
            
            for property,value in pairs(secondParameter) do
                instance[property] = value
            end
        else
            instance.Parent = secondParameter
        end
        
        return instance
    end
}

However, as it’s not the RbxUtility.Create, you won’t have to worry about a random update breaking it :wink:

For easy usage – replace the default Instance with this.

Example usage:

Instance = require(182880820)
local part = Instance.new("Part", {
Size = Vector3.new(0.5,0.5,0.5);
FormFactor = "Custom";
Parent = workspace;
})

or plain old

Instance = require(182880820)
Instance.new("Part", workspace)

The module isn’t dumb and will set the FormFactor if it’s there before setting the size, so you won’t have to worry about hash tables messing up the order that the data is stored in and resulting in Size not being set after FormFactor.

  1. No child support? waaaaat?

  2. I totally prefer the

Create 'className' {prop=val}

format, because you don’t have to stack up a million })})})}) at the end of your call whenever you want to create something like a gui.

“1) No child support? waaaaat?”
?

“Create ‘className’ {prop=val}”

Can you give me an example of that creating something and setting ~5 properties – I haven’t used Create that much because of the bad things I’ve heard about it.

Also, added Instance.clone(originalInstance, secondParameter)

Returns a clone of the original instance, parented to the second parameter if it’s not a table or sets the properties of the clone based on what’s in the table.

Child support means you can do this:

[code]Create ‘className’ {
a=b,
b=c,

workspace,
Part,
thing
}[/code]
And workspace, Part, and thing will all be parented to the part you’re creating. You can stack calls to Create in this way to make nice clean towers of code.

This is the create function I use. This one also has function support, meaning you supply a function and then the function is called with the object as an argument.

[code]local function Create(class, defaultParent)
return function(list)
local obj = Instance.new(class)

	for index, value in next, list do
		if type(index) == 'string' then
			-- set property
			obj[index] = value
		elseif type(index) == 'number' then
			if type(value) == 'userdata' then
				-- set parent
				value.Parent = obj
			elseif type(value) == 'function' then
				value(obj)
			end
		end
	end

	obj.Parent = obj.Parent or defaultParent

	return obj
end

end[/code]
Example usage:

[code]Create (‘Part’, workspace) {
Name = ‘adsf’,

Create ‘StringValue’ {
Name = ‘Stringssss’,
Value = ‘2’
}
}[/code]

Alright – added child/function support.

btw what are some examples where function support is useful? It seems useful, but I can’t picture using it for anything.