confetti
This is a simple confetti module that I made, great performance and appealing.
It avoids creating instances but instead, uses a pool of instances that can be reused again and again. Greatly enhancing the performance of this effect, meaning, you can spam it, and it will not lag your game.
confetti.luau:
--!strict
type activepiece = {
[number]: number | Frame
}
-- change it however you want
local poolSize = 200
local gravity = 2.2
local spreadForce = 1.0
local upwardForce = -2.8
local minimumSize = 40
local maximumSize = 50
-- some colors for the confetti
local colors = {
Color3.fromRGB(255, 87, 87),
Color3.fromRGB(255, 195, 77),
Color3.fromRGB(255, 255, 102),
Color3.fromRGB(102, 255, 102),
Color3.fromRGB(102, 178, 255),
Color3.fromRGB(178, 102, 255),
Color3.fromRGB(255, 102, 255),
}
local random = math.random
local newUDim2 = UDim2.new
local insert = table.insert
local remove = table.remove
local pool: {Frame} = {}
local activePieces: {activepiece} = {}
runService.RenderStepped:Connect(function(deltaTime: number)
if deltaTime > 0.1 then
deltaTime = 0.1
end
for i = #activePieces, 1, -1 do
local data = activePieces[i]
local x = data[1] :: number
local y = data[2] :: number
local velocityX = data[3] :: number
local velocityY = data[4] :: number
local rotation = data[5] :: number
local rotationVelocity = data[6] :: number
local piece = data[7] :: Frame
velocityY += gravity * deltaTime
x += velocityX * deltaTime
y += velocityY * deltaTime
rotation += rotationVelocity * deltaTime
data[1] = x
data[2] = y
data[4] = velocityY
data[5] = rotation
piece.Position = newUDim2(x, 0, y, 0)
piece.Rotation = rotation
if y > 1.1 or x < -0.1 or x > 1.1 then
piece.Visible = false
insert(pool, piece)
remove(activePieces, i)
end
end
end)
-- call this function to play the effect
function confetti.play(amount: number?)
local count = amount or 150
local min = minimumSize
local max = maximumSize
for _ = 1, count do
local piece = remove(pool)
if not piece then
break
end
local size = random(min, max)
piece.Size = newUDim2(0, size, 0, size)
piece.BackgroundColor3 = colors[random(1, #colors)]
piece.Visible = true
insert(activePieces, {
[1] = random(0, 1000) / 1000, -- x position
[2] = 1.05, -- y position
[3] = (random() - 0.5) * spreadForce,
[4] = random(upwardForce * 100, (upwardForce * 0.4) * 100) / 100,
[5] = random(-180, 180),
[6] = random(-500, 500),
[7] = piece
})
end
end
-- run this function when you require this module
function confetti.setup()
for _ = 1, poolSize do
local piece = Instance.new("Frame")
piece.AnchorPoint = Vector2.new(0.5, 0.5)
piece.BorderSizePixel = 0
piece.Visible = false
piece.Parent = Confetti.UI
insert(pool, piece)
end
end