Always a way, idk if shorter but definitely nicer:
local function newObj(type, props)
local obj = Instance.new(type)
for PropName, Prop in props do
if obj[PropName] then obj[PropName] = Prop end
end
return obj
end
local Part = newObj("Part", {
["Parent"] = workspace
})
Pass in the properties in a table as the second argument of the function, make sure it’s one table and has the correct Property Names and the values you want them to have. The first parameter is the type of object you want to make with the function (here we are using "Part" but you can use anything that Instance.new can make)
Not sure if I understand what you’re trying to achieve but something like this might work:
local function AddItem(ClassName:string, Properties:{[string]: any})
local obj = Instance.new(ClassName)
for i,v in pairs(Properties)
obj[i] = v
end
return obj
end
--Example usage
local Part = AddItem("Part", {Parent = workspace, Name = "Test"})
Ah, I see. You could try using a function to create this effect:
function newItem(Type,Properties)
local obj = Instance.new(Type)
for i,v in pairs(Properties) do
if i ~= "Parent" then -- setting properties after parenting is bad for performance
obj[i] = v
end
end
obj.Parent = Properties["Parent"] -- If there's no parent here, it won't error because it'll be nil
return obj
end
local part = newItem("Part",{
CFrame = ehrp.CFrame;
Material = Enum.Material.Neon;
CanCollide = false;
Shape = Enum.PartType.Ball;
Anchored = true;
Transparency = 1;
Color = Color3.new(1,1,1);
Size = Vector3.new(10,10,10);
})
Keep in mind, shorter code isn’t exactly better code. You may want to leave it expanded