Hello, so I have this confetti module and I was wondering how do I make it play once someone purchased a gamepass or dev product from my game? I’m new to scripting so please explain a lot and code is appreciated.
Module Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local camera = workspace.CurrentCamera
local confettiShapesAssets = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("ConfettiShapes")
local confettiShapes = {
["Circle"] = 20,
["Square"] = 20,
["Triangle"] = 20,
["Heart"] = 10,
["Star"] = 5,
["Diamond"] = 5,
}
local function getRandomConfettiShape()
local totalWeight = 0
for _, weight in confettiShapes do
totalWeight += weight
end
local r = math.random(totalWeight)
local count = 0
for shape, weight in confettiShapes do
count += weight
if r <= count then
return confettiShapesAssets:FindFirstChild(shape)
end
end
end
local UIConfetti = {}
UIConfetti.__index = UIConfetti
type ConfettiOptions = {
Position: Vector2,
Force: Vector2,
Gravity: Vector2,
Parent: GuiBase,
Colors: { [number]: Color3 }?,
}
function UIConfetti.new(options: ConfettiOptions)
if not options then
return
end
local self = setmetatable({}, UIConfetti)
local xForce = if options.Force.X < 0 then options.Force.X * -1 else options.Force.X
options.Force = Vector2.new(options.Force.X, options.Force.Y + ((0 - xForce) * 0.8))
local colorsList
if not options.Colors then
-- default colors
options.Colors = {
Color3.fromRGB(168, 100, 253),
Color3.fromRGB(41, 205, 255),
Color3.fromRGB(120, 255, 68),
Color3.fromRGB(255, 113, 141),
Color3.fromRGB(253, 255, 106),
}
end
colorsList = options.Colors
self.Gravity = options.Gravity or Vector2.new(0, 1)
self.EmitterPosition = options.Position
self.EmitterPower = options.Force
self.Position = Vector2.new(0, 0)
self.Power = options.Force
self.Colors = options.Colors
self.CurrentColor = colorsList[math.random(#colorsList)]
local function getParticle()
local label = getRandomConfettiShape():Clone()
label.ImageColor3 = self.CurrentColor
label.Parent = options.Parent
label.Rotation = math.random(360)
label.Visible = false
label.ZIndex = 20
return label
end
self.Label = getParticle()
self.DefaultSize = 30
self.Size = 1
self.Side = -1
self.OutOfBounds = false
self.Enabled = false
self.Cycles = 0
return self
end
function UIConfetti:Update()
if self.Enabled and self.OutOfBounds then
self.Label.ImageColor3 = self.CurrentColor
self.Position = Vector2.new(0, 0)
self.Power = Vector2.new(self.EmitterPower.X + math.random(10) - 5, self.EmitterPower.Y + math.random(10) - 5)
self.Cycles = self.Cycles + 1
end
if (not self.Enabled and self.OutOfBounds) or (not self.Enabled and (self.Cycles == 0)) then
self.Label.Visible = false
self.OutOfBounds = true
self.CurrentColor = self.Colors[math.random(#self.Colors)]
return
else
self.Label.Visible = true
end
local startPosition, currentPosition, currentPower = self.EmitterPosition, self.Position, self.Power
local imageLabel = self.Label
if imageLabel then
-- position
local newPosition = Vector2.new(currentPosition.X - currentPower.X, currentPosition.Y - currentPower.Y)
local newPower = Vector2.new((currentPower.X / 1.05) - self.Gravity.X, (currentPower.Y / 1.05) - self.Gravity.Y)
local ViewportSize = camera.ViewportSize
imageLabel.Position = UDim2.new(startPosition.X, newPosition.X, startPosition.Y, newPosition.Y)
self.OutOfBounds = (imageLabel.AbsolutePosition.X > ViewportSize.X and self.Gravity.X > 0)
or (imageLabel.AbsolutePosition.Y > ViewportSize.Y and self.Gravity.Y > 0)
or (imageLabel.AbsolutePosition.X < 0 and self.Gravity.X < 0)
or (imageLabel.AbsolutePosition.Y < 0 and self.Gravity.Y < 0)
self.Position, self.Power = newPosition, newPower
-- spin
if newPower.Y < 0 then
if self.Size <= 0 then
self.Side = 1
imageLabel.ImageColor3 = self.CurrentColor
end
if self.Size >= self.DefaultSize then
self.Side = -1
imageLabel.ImageColor3 =
Color3.new(self.CurrentColor.r * 0.9, self.CurrentColor.g * 0.9, self.CurrentColor.b * 0.9)
end
self.Size = self.Size + (self.Side * 2)
imageLabel.Size = UDim2.new(0, self.DefaultSize, 0, self.Size)
end
end
end
function UIConfetti:Toggle()
self.Enabled = not self.Enabled
end
function UIConfetti:Destroy()
self.Label:Destroy()
table.clear(self)
setmetatable(self, nil)
end
return UIConfetti
Example Local Script Code
local RunService = game:GetService('RunService')
local UIConfetti = require(script.Parent.UIConfetti)
local createdConfetti = {}
-- Made by kyexyz
-- The confetti module is pretty customizable so have fun with it :)
--[[
Confetti Options:
Position - Vector2 - confetti position in scale (x, y)
Force - Vector2 - force that will be applied to the confetti
Gravity - Vector2 - gravity applied to the confetti with the force
Parent - GuiBase - the confetti will be parented to this
Colors - {Color3} - the colors for the confetti (random one will be picked from the given list)
]]
-- I have included two examples!
for i = 1, 50 do
-- confetti rain
--local new = UIConfetti.new({
-- Position = Vector2.new(math.random(3, 8) / 10, -0.25),
-- Force = Vector2.new(math.random(-50, 50), -math.random(10, 25)),
-- Gravity = Vector2.new(0, 0.25),
-- Parent = script.Parent.Frame,
--})
-- confetti blasting up
local new = UIConfetti.new({
Position = Vector2.new(0.5, 1),
Force = Vector2.new(math.random(-50,50), math.random(50,100)),
Gravity = Vector2.new(0, 1),
Parent = script.Parent.Frame,
})
table.insert(createdConfetti, new)
task.wait(0.05)
end
script.Parent.Frame.Visible = true
local confettiActive = false
local t = tick()
RunService.RenderStepped:Connect(function()
if (tick() - t) > 10 then
t = tick()
task.spawn(function()
confettiActive = true
task.wait(0.5)
confettiActive = false
end)
end
for _, val in pairs(createdConfetti) do
val.Enabled = confettiActive
val:Update()
end
end)