Feedback on Notification Queue System

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.

6 Likes

Just have it fire a function when there is a new notification or wherever somethings being added or removed from the list.

For example:

local list = {}

function changed() -- When something changing to the list
    if #list > 0 then
	-- blah blah blah
    end
end

function notification(blah)
    table.insert(list,blah)
    changed()
end
2 Likes

But if the list is not empty then it wouldn’t be able to wait for the current notification to be complete? Wouldn’t that end up with the same loop or would it be more efficient to have the individual loop in the function?

That’s why I said fire it whenever something is added or removed, that way when one notification is done and removed from the list then it will fire again and check if there any more notifications.

Edit:
That way it will be a constant cycle and only check if it’s necessary and improve performance as it’s not a while loop that is endless.

2 Likes

Oh right I see what you mean, I’ll see what I can do.

I think @metalive ‘s solution is the way you want to go.

The function should recursively call itself, popping elements off the queue until no notifications remain. This function can also manage a boolean variable signifying wether or not it is running.

When a notification is added, check if the recursive function is running by looking at the variable mentioned above. If it isn’t running, call it and it will run until there are no elements left in the queue.

2 Likes

hi, it feels weird replying to a 4 year old post, but anyway…

I have tried to use this method in my notification system, and it doesn’t wait until the previous notification has done all it needs to do.

Say if I fire the remote event every 2 seconds, the notification show up every 2 seconds, rather than waiting in the queue until the previous notification has completed its 5 second animations and stuff related to it.

The server fires to all clients, which is when I insert the info into the table.

How would I script it so that the notifications actually wait until the previous one has finished all it needs to do?