How would I make a notification system that has anti-duplicates?

I have this working text system however, if the same text label appears any amount of times, it clutters the screen. I want to know how to make it so that when a duplicate appears, it says “Message” (x2) or (x3) and beyond depending on the amount of duplicates.

Could anyone give me an example?

local Notify = {}

function Notify.CreateNewLabel(text, color)--module script
	task.spawn(function()
		local label = script.Parent.Preset:Clone()
		local stroke = label:FindFirstChildWhichIsA("UIStroke")
		label.Name = "Active"
		script.Parent.Click:Play()
		label.Parent = script.Parent.Text
		label.Text = text
		label.TextColor3 = color
		for count = 20, 0, -1 do
			label.TextTransparency -= 0.1
			stroke.Transparency -= 0.1
			task.wait(0.05)
		end
		task.wait(3)
		for count2 = 20, 0, -1 do
			label.TextTransparency += 0.1
			stroke.Transparency += 0.1
			task.wait(0.05)
		end
		task.wait(0.5)
		label:Destroy()
	end)
end

return Notify

The frame uses a UIListLayout to make sure the text labels don’t go inside of eachother.
image

You can add a counter for each text label that is created and increment it every time a label with the same text is created. You can then use this counter to append “(x2)”, “(x3)”, etc. to the text of the label if the counter is greater than 1.

1 Like

How would I find if a new text label is created?

You can use a table counters to keep track of the number of times each text label has been created. When a new text label is created, you can increment the corresponding count in the counters table, and use the count value to display the number of duplicates.

How do I add values to a table?

local Notify = {}
local counters = {}

function Notify.CreateNewLabel(text, color)
	local count = counters[text] or 0
	counters[text] = count + 1

	local label = script.Parent.Preset:Clone()
	local stroke = label:FindFirstChild("UIStroke")
	label.Name = "Active"
	script.Parent.Click:Play()
	label.Parent = script.Parent.Text
	if count > 0 then
		label.Text = text .. " (x" .. (count + 1) .. ")"
	else
		label.Text = text
	end
	label.TextColor3 = color
	label.TextTransparency = 1

	local function fadeIn()
		for i = 1, 20 do
			label.TextTransparency = label.TextTransparency - 0.05
			stroke.Transparency = stroke.Transparency - 0.05
			wait(0.05)
		end
	end

	local function fadeOut()
		for i = 1, 20 do
			label.TextTransparency = label.TextTransparency + 0.05
			stroke.Transparency = stroke.Transparency + 0.05
			wait(0.05)
		end
		label:Destroy()
		counters[text] = count
	end

	fadeIn()
	wait(3)
	fadeOut()
end

return Notify

here’s an updated version of the code, lmk if it works.

Edit: Here’s more info about tables. Tables | Roblox Creator Documentation

1 Like

How would I make it so that if the count is > 0, the preset doesn’t get duplicated and it just changes the text of the already existing one.

And thanks for the info about tables.

1 Like

Ofc, and abt that you can do this by:

local Notify = {}
local messageCount = {}

function Notify.CreateNewLabel(text, color)
	if messageCount[text] == nil then
		messageCount[text] = 0
	end

	messageCount[text] = messageCount[text] + 1

	task.spawn(function()
		local label
		if messageCount[text] == 1 then
			label = script.Parent.Preset:Clone()
			label.Name = "Active"
			label.Parent = script.Parent.Text
		else
			label = script.Parent.Text:FindFirstChild("Active")
		end

		script.Parent.Click:Play()

		local stroke = label:FindFirstChildWhichIsA("UIStroke")
		label.Text = text .. " (x" .. messageCount[text] .. ")"
		label.TextColor3 = color

		for count = 20, 0, -1 do
			label.TextTransparency -= 0.1
			stroke.Transparency -= 0.1
			task.wait(0.05)
		end

		task.wait(3)

		for count2 = 20, 0, -1 do
			label.TextTransparency += 0.1
			stroke.Transparency += 0.1
			task.wait(0.05)
		end

		messageCount[text] = messageCount[text] - 1
		if messageCount[text] == 0 then
			task.wait(0.5)
			label:Destroy()
		end
	end)
end

return Notify

  1. We’ve added a table messageCount to keep track of how many times a message has been displayed.

  2. When Notify.CreateNewLabel is called its going to first check the messageCount table to see if a message with that text has already been displayed. If it hasn’t, a new label is created and its count is set to 1. If it has, the existing label is modified and its count is incremented. The count is appended to the label text so that you can see how many times the same message has been displayed.

At the end of the task, the count for that message is decremented, and if the count reaches 0, the label is destroyed.

1 Like

NEVERMIND, I forgot to remove a line of code which clashed with my statement. It still doesn’t work with multiple text labels though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.