Any way to make this function shorter?

Is there any methods to make this function more shorter? I’m not sure if I understand how to do tables {}

			local part1 = Instance.new("Part")
			local part2 = Instance.new("Part")
			part1.CFrame=ehrp.CFrame
			part1.Material="Neon"
			part1.CanCollide=false
			part1.Shape="Ball"
			part1.Anchored=true
			part1.Transparency=1
			part1.Color=Color3.new(1,1,1)
			part1.Size=Vector3.new(10,10,10)
			part2.CFrame=ehrp.CFrame
			part2.Material="Neon"
			part2.CanCollide=false
			part2.Shape="Ball"
			part2.Anchored=true
			part2.Transparency=1
			part2.Color=Color3.new(1,1,1)
			part2.Size=Vector3.new(10,10,10)

I want to keep it similar as this code I’m used to writing and seeing. Was wondering if I could do the same with instance.new with at least 2-3 lines.

.additem("Part",{CFrame=ehrp.CFrame,Material="Neon",CanCollide=false,Shape="Ball",Anchored=true,Transparency=1,Color=Color3.new(1,1,1),Size=Vector3.new(10,10,10)})

I don’t care much about looking neat, no one gonna see them but me anyways :slight_smile:

1 Like

Assuming you mean lines,

doing this should help:

local part1 = Instance.new("Part"); local part2 = Instance.new("Part")

Only issue, looks ugly

2 Likes

Any shorter way for properties as well for individual instances?

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)

2 Likes

I’m not sure, I’m more used to creating codes like this, I was just wondering if I could do the same with instances or at least two lines.

.additem("Part",{CFrame=ehrp.CFrame,Material="Neon",CanCollide=false,Shape="Ball",Anchored=true,Transparency=1,Color=Color3.new(1,1,1),Size=Vector3.new(10,10,10)})

Oh, I thought you just cared about keeping the code clean, less lines isnt always better keep in mind. Like here your sacrificing readability

1 Like

It’s not like anyone is gonna see them xd
and I’m most used to seeing something like these.

1 Like

This kind of Dumb script i made after playing around, still post it here anyways lol

Table = {
	Color = Color3.fromRGB(0,0,0),
}

game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(0), Table):Play()

1 Like

You will see them. Also I have no idea where you’ve seen all the properties set on one line but sure :man_shrugging:

1 Like

What if the property is different for each part? Usually, I think tweenservice would just be getting the part to its goal and not immediate.

I need something that is immediate execution. Even if the delay is 0.

Well, I kinda zoom out a lot, the wordings are small but I’m able to see everything :slight_smile:

Since these parts are exactly the same, you could create one then clone it:

local part1 = Instance.new("Part")
part1.CFrame=ehrp.CFrame
part1.Material="Neon"
part1.CanCollide=false
part1.Shape="Ball"
part1.Anchored=true
part1.Transparency=1
part1.Color=Color3.new(1,1,1)
part1.Size=Vector3.new(10,10,10)
local part2 = part1:Clone()
1 Like

But what if the properties are the same? I’m looking for the ones that keeps all the properties on the individuals all in one or two lines

Can you elaborate on this? These are the same, so you can clone it to get an exact copy.

1 Like

I have reedit the post, I want the code something similar to

.additem("Part",{CFrame=ehrp.CFrame,Material="Neon",CanCollide=false,Shape="Ball",Anchored=true,Transparency=1,Color=Color3.new(1,1,1),Size=Vector3.new(10,10,10)})

I want to do the same with properties that are different from each other too.

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"})

2 Likes

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

1 Like

maybe not specifically shorter, but a far better and more readable way would be something like this

local function genParts()
	local numOfParts = 2
	
	local partsA = {}
	
	for i = 1, numOfParts, 1 do
		local part = Instance.new("Part")
		
		part.CFrame = CFrame.new(0,5,0)--ehrp.CFrame
		part.Material = "Neon"
		part.CanCollide = false
		part.Shape = "Ball"
		part.Anchored = true
		part.Transparency = 1
		part.Color = Color3.new(1,1,1)
		part.Size = Vector3.new(10,10,10)
		part.Name = "Part".. tostring(i)
		part.Parent = workspace
		
		table.insert(partsA, part)
	end
	
	return partsA[1], partsA[2]
end

local part1, part2 = genParts()
1 Like

Although it’s not short, I think this is a more viable option. Thanks.

This one also works as well similarly to the solution. I thank you for that.