The code I’ve written is a custom notification system written in a module, the client will detect when it wants to request a notification and send it. This all works fine, my main issue is the constant checking if a notification is in a queue. Is there a better way of doing this that I haven’t thought of? Is there any other ways I can improve this code?
-- Client > Module
Notifications.RequestNotification({
_text = {title = 'LEVELED UP!', subText = 'LEVEL '..Level.Value},
_type = 'TopMiddle',
_time = 5
})
-- Module
local Player = game:GetService('Players').LocalPlayer
repeat wait() until Player.PlayerGui
local UI = Player.PlayerGui:WaitForChild('Notifications')
local NotificationTypes = {'TopMiddle', 'BottomMiddle'}
--local NotificationQueue = require(script.Parent:WaitForChild'Queue')
local playing_notification = false
local module = {}
local queue = {}
module.NewNotification = function(args)
local _type = args._type
local _time = args._time
local _text = args._text
local frame = UI:FindFirstChild(_type)
if not frame then
return false
end
frame:TweenPosition(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, .5) wait(.5)
-- start {0, 0},{-0.5, 0}
-- end {0, 0},{0, 0}
local sound_effect = script._unlock
if sound_effect.IsLoaded then
sound_effect:Play()
end
if _text.title then
frame.title.Text = _text.title
end
if _text.subText then
frame.subText.Text = _text.subText
end
frame.Visible = true
wait(_time or 2)
frame:TweenPosition(UDim2.new(0,0,-0.5,0), Enum.EasingDirection.In, Enum.EasingStyle.Bounce, 0.5) wait(.5)
frame.Visible = false
return true
end
spawn(function()
while true do
wait(.5)
if #queue > 0 then
if not playing_notification then
playing_notification = true
module.NewNotification(queue[1])
table.remove(queue, 1)
playing_notification = false
end
end
end
end)
module.RequestNotification = function(args)
if UI:FindFirstChild(args._type) then
table.insert(queue, #queue + 1, args)
end
end
return module
Any feedback is appreciated, thanks.