I made this 3D confeti particle script

Looking for feedback!

local ConfetiCount = 40

for a = 1, ConfetiCount do
    local Confeti = Instance.new("Part")
    Confeti.Size = Vector3.new(0.8, 0, 0.3)
    Confeti.Material = Enum.Material.SmoothPlastic
    Confeti.CanCollide = false
    Confeti.Position = script.Parent.Position
    Confeti.CastShadow = false
    
    local RandomColor = math.random(1,6)
    
    if RandomColor == 1 then
        Confeti.Color = Color3.new(1, 0, 0)
    elseif RandomColor == 2 then
        Confeti.Color = Color3.new(0, 1, 0)
    elseif RandomColor == 3 then
        Confeti.Color = Color3.new(0, 0, 1)
    elseif RandomColor == 4 then
        Confeti.Color = Color3.new(0, 1, 1)
    elseif RandomColor == 5 then
        Confeti.Color = Color3.new(1, 1, 0)
    elseif RandomColor == 6 then
        Confeti.Color = Color3.new(1, 0, 1)
    end
    
    local ConfetiBV = Instance.new("BodyVelocity",Confeti)
    local ConfetiBAV = Instance.new("BodyAngularVelocity",Confeti)
    
    ConfetiBV.MaxForce = Vector3.new(1,1,1)*math.huge
    ConfetiBAV.MaxTorque = Vector3.new(1,1,1)*math.huge
    
    ConfetiBV.Velocity = Vector3.new(math.random(-100,100),math.random(70,100),math.random(-100,100))
    ConfetiBAV.AngularVelocity = Vector3.new(math.random(-15,15),math.random(-15,15),math.random(-15,15))
    
    Confeti.Parent = workspace
    
    game:GetService("TweenService"):Create(ConfetiBV, TweenInfo.new(1,Enum.EasingStyle.Exponential),{Velocity = Vector3.new(0,-5,0)}):Play()
    
    task.delay(3,function()
        if Confeti then
            game:GetService("TweenService"):Create(Confeti,TweenInfo.new(1),{Transparency = 1}):Play()
            task.delay(1,function()
                if Confeti then
                    Confeti:Destroy()
                end
            end)
        end
    end)
end

Super fun script:> good job. I tried making it launch Traffic Cones. Here’s your code changed a bit if you wanna check it out:> just a little change

local ConfetiCount = 40
local IS = game:GetService("InsertService")

for a = 1, ConfetiCount do
	local TempTrafficCone = IS:LoadAsset(23153487)
	TempTrafficCone.Parent = game.ReplicatedStorage
	TempTrafficCone.Name = "TrafficConeHold"
	
	local TrafficCone = game.ReplicatedStorage:FindFirstChild("TrafficConeHold"):FindFirstChild("Traffic Cone"):Clone()
	TrafficCone.Size = Vector3.new(1.2, 0.1, 1.2)
	TrafficCone.Material = Enum.Material.SmoothPlastic
	TrafficCone.CanCollide = false
	TrafficCone.Transparency = 0
	TrafficCone.Position = script.Parent.Position
	TrafficCone.CastShadow = true

	local ConfetiBV = Instance.new("BodyVelocity",TrafficCone)
	local ConfetiBAV = Instance.new("BodyAngularVelocity",TrafficCone)

	ConfetiBV.MaxForce = Vector3.new(1,1,1)*math.huge
	ConfetiBAV.MaxTorque = Vector3.new(1,1,1)*math.huge

	ConfetiBV.Velocity = Vector3.new(math.random(-100,100),math.random(70,100),math.random(-100,100))
	ConfetiBAV.AngularVelocity = Vector3.new(math.random(-15,15),math.random(-15,15),math.random(-15,15))

	TrafficCone.Parent = workspace

	game:GetService("TweenService"):Create(ConfetiBV, TweenInfo.new(1,Enum.EasingStyle.Exponential),{Velocity = Vector3.new(0,-5,0)}):Play()

	task.delay(3,function()
		if TrafficCone then
			game:GetService("TweenService"):Create(TrafficCone,TweenInfo.new(1),{Transparency = 1}):Play()
			task.delay(1,function()
				if TrafficCone then
					TrafficCone:Destroy()
					TempTrafficCone:Destroy()
				end
			end)
		end
	end)
end

Cool script! My feedback:

  1. Creating instances and parenting them in a single call (Instance.new(name, parent)) is strongly discouraged.
  2. Instead of long chain of if and elseif statements, you can put colors into an array and pick a random index, which then you can use to retrieve a random color.
  3. You are not actually checking whether the instance exists, the Confeti variable holds a reference to a part, the value will not change on its own, ever. Remove those redundant checks.
  4. Both Vector3.new(1,1,1)*math.huge and game:GetService("TweenService") can be cached in a variable and reused.
  5. Avoid squeezing too much logic into a single line, as it makes it hard to read (at least for me).
  6. For the fade out tween, you can wait for its completion, via the Completed event instead of waiting with separate mechanism.
  7. If you don’t have a name for a variable that is unused, especially in a for loop, like the a, you can name it _.
  8. You can use Vector3.one instead of Vector.new(1, 1, 1).
  9. The script can be turned into reusable function which can be placed in a ModuleScript, and types can be added for improved readability.

Example:

local colors: {Color3} = {
	Color3.new(1, 0, 0),
	Color3.new(0, 1, 0),
	Color3.new(0, 0, 1),
	Color3.new(0, 1, 1),
	Color3.new(1, 1, 0),
	Color3.new(1, 0, 1)
}

local hugeVector3 = Vector3.one * math.huge
local tweenService = game:GetService("TweenService")

local function launchConfetti(origin: Vector3, count: number?): ()
	for _ = 1, count or 40 do
		local part = Instance.new("Part")
		part.Size = Vector3.new(0.8, 0, 0.3)
		part.Material = Enum.Material.SmoothPlastic
		part.CanCollide = false
		part.CastShadow = false
		part.Position = origin
		part.Color = colors[math.random(#colors)]

		local bodyVelocity = Instance.new("BodyVelocity")
		local bodyAngularVelocity = Instance.new("BodyAngularVelocity")

		bodyVelocity.MaxForce = hugeVector3
		bodyAngularVelocity.MaxTorque = hugeVector3

		bodyVelocity.Velocity = Vector3.new(
			math.random(-100, 100),
			math.random(70, 100),
			math.random(-100, 100)
		)

		bodyAngularVelocity.AngularVelocity = Vector3.new(
			math.random(-15, 15),
			math.random(-15, 15),
			math.random(-15, 15)
		)

		bodyVelocity.Parent = part
		bodyAngularVelocity.Parent = part
		part.Parent = workspace

		tweenService:Create(bodyVelocity, TweenInfo.new(1, Enum.EasingStyle.Exponential), {
			Velocity = Vector3.new(0, -5, 0)
		}):Play()

		task.delay(3, function()
			local fadeOut = tweenService:Create(part, TweenInfo.new(1), {
				Transparency = 1
			})

			fadeOut:Play()
			fadeOut.Completed:Wait()
			part:Destroy()
		end)
	end
end

launchConfetti(script.Parent.Position)

I might be a little late, but I thought I would still share for future readers.

Vector would change nothing; the compiler would precompile it regardless (at least with the native vector Luau library, unsure about Vector3).
Etc this is the most optimized way:

local inf = vector.create(math.huge,math.huge,math.huge)

Caching could utilize MOVE and so would benefit in some cases.

Same problem here
Vector3.zero would utilize GETIMPORT, but Vector3.new(1,1,1) would be precompiled with LOADK.
Theoretically:

local vector_one = vector.create(1,1,1)

Would be the fastest

That depends
If the instance you are creating does not have properties that change upon container OR you create an instance with default property, then Instance.new(Class, Parent) absolutely wins performance-wise.
Adding a register to the function call stack would be so much cheaper than doing 2 Luau → Roblox API calls.

Once again, not everything is black and white;
Indexing a table (even when it’s an array) is taking a lot of resources.
OP should’ve precompiled Color3 instead.

No naming imperialism, please; even The New Luau Order does not tolerate such violation of liberty. >:(
If the compiler does not care, so shall you.

Hell nah, this one is a fat no. :-1:
Balkanization and deoptimization is a root of all evil.

You could simplify the color selection like this:

local R = 0
local G = 0
local B = 0

if math.random(1,2) == 1 then
	R = 1
	if math.random(1,2) == 1 then
		G = 1
	elseif math.random(1,2) == 1 then
		B = 1
	end
else
	if math.random(1,2) == 1 then
		G = 1
		if math.random(1,2) == 1 then
			B = 1
		end
	else
		B = 1
	end
end

local RandomColor = Color3.new(R,G,B)

BodyVelocity and BodyAngularVelocity are deprecated instances, use LinearVelocity and AngularVelocity as replacements.