StarsEmitter - Emits stars in your UI!

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.

Image from Gyazo

πŸ“ƒ 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)

:heart: Likes are very much appreciated!

32 Likes

That’s really cool, nice work!

2 Likes

Looks pretty good!

By the way, instead of doing:

You can do:

fadeInTween:Play()
fadeInTween.Completed:Wait()
fadeOutTween:Play()
fadeOutTween.Completed:Wait()
4 Likes

This looks really cool, I might use this in my project

One thing I suggest changing for performance:

Instead of looping through every position to check the distance, you could instead round the random (x,y) to 0.1, so then you can directly index the table to check if the rounded position is already taken.


So this:

Would become something like

And checking if the position already exists would be as easy as:

and to remove the (x,y) from being used:

1 Like

I have been looking for this for a long time!

Hi,
can you post the whole code in one block with what you suggest to use?

@MattyDevs can you make more effects, you’re really good!