User:NXTBoy/Scripts/"new" operator

From Legacy Roblox Wiki
Revision as of 09:25, 15 September 2011 by >NXTBoy (New page: <pre> _G.new = function(typeName) return function(properties) pcall(function local i = Instance.new(typeName) if type(properties) == "table" then for property, value in pairs(pro...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
_G.new = function(typeName)
	return function(properties)
		pcall(function
		local i = Instance.new(typeName)
		if type(properties) == "table" then
			for property, value in pairs(properties) do
				if type(property) == "number" then
					value.Parent = i
				else
					i[property] = value
				end
			end
		end
		return i
	end
end

Usage as follows:

local new = _G.new


local m = new "Model" {
	Name = "Test",
	Parent = workspace,

	new "Part" {
		Name = "My Part",
		BrickColor = BrickColor.Red(),
		Size = Vector3.new(2, 2, 2)
	}
}

local p = new "Part"()
p.Parent = m