StarsEmitter - Emits stars in your UI!
Hello everyone.
I made a module script which adds stars (or any other images you want) to your UI.
Very user-friendly and easy to use.
π Source Code:
--[[
@author MattyDevs
StarsEmitter.lua
--]]
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarsEmitter = {}
StarsEmitter.__index = StarsEmitter
function StarsEmitter.new(parent: Instance, spawnRate: number)
local self = setmetatable({}, StarsEmitter)
self.Parent = parent
self.usedPositions = {}
self.spawnRate = spawnRate or 0.4
self:Init()
return self
end
function StarsEmitter:Create()
local Star = script.Star:Clone()
local x, y, starSize
repeat
x, y = math.random(0, 1000) / 1000, math.random(0, 1000) / 1000
until not self:IsPositionUsed(x, y)
starSize = math.random(150, 250) / 1000
table.insert(self.usedPositions, {x = x, y = y})
Star.Size = UDim2.new(0, 0, 0, 0)
Star.Position = UDim2.new(x, 0, y, 0)
Star.Parent = self.Parent.Stars
local fadeInTween = TweenService:Create(Star, TweenInfo.new(0.85), {Size = UDim2.new(starSize, 0, starSize, 0)})
local fadeOutTween = TweenService:Create(Star, TweenInfo.new(0.85), {Size = UDim2.new(0, 0, 0, 0)})
fadeInTween:Play()
task.wait(0.85)
fadeOutTween:Play()
task.wait(0.85)
spawn(function()
for i, position in ipairs(self.usedPositions) do
if position.x == x and position.y == y then
table.remove(self.usedPositions, i)
break
end
end
end)
Star:Destroy()
end
function StarsEmitter:IsPositionUsed(x, y)
for _, position in ipairs(self.usedPositions) do
if math.abs(x - position.x) < 0.1 and math.abs(y - position.y) < 0.1 then
return true
end
end
return false
end
function StarsEmitter:Init()
local StarsFolder = Instance.new("Folder", self.Parent)
StarsFolder.Name = "Stars"
spawn(function()
while task.wait() do
spawn(function()
self:Create()
end)
task.wait(self.spawnRate)
end
end)
end
return StarsEmitter
StarsEmitterExample.rbxm (7.5 KB)
Likes are very much appreciated!