Problems with In-Game Custom Notification System

I wrote a basic notification system for my game recently and the fundamentals work nicely. The notification is played, and then it moves off of the screen and disappears for good. That’s how it’s supposed to work, right?

Apparently not, because there seems to be a bit of a vulnerability that I’ve been battling for the last week.

If you try to activate a notification as another one is leaving, the script errors, breaking it permanently. There’s no way of fixing it from there.

Photo of error given:

Video demo attached below:

EDIT: when clicking on this link, the video is downloaded to your device. It was uploaded to Streamable, and I am not sure I can fix this.

External Media

Everything is explained within the clip, so please watch it before making a statement.

If the script is needed, here (it is a LocalScript):

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local TweenService = game:GetService("TweenService")
local NotificationEvent = game:GetService("ReplicatedStorage"):WaitForChild("Notification")
local NotificationGui = script.NotificationGui
local Active = script.ActiveNotifications

local notificationguis = {}


local TweenInf = TweenInfo.new(
	0.6,
	Enum.EasingStyle.Back,
	Enum.EasingDirection.Out,
	0,
	false
)

local TweenInf2 = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.In,
	0,
	false
)
local TweenInf3 = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.Out,
	0,
	false
)

NotificationEvent.OnClientEvent:Connect(function(label, text)
	local gui = NotificationGui:Clone()
	gui.Parent = PlayerGui
	table.insert(notificationguis,gui)
	if Active.Value >=1 then
		for _,ui in notificationguis do
			TweenService:Create(ui.notification, TweenInf3, {Position = UDim2.new(ui.notification.Position.X.Scale, 0, ui.notification.Position.Y.Scale-0.18, 0)}):Play() -- This is the line the error always falls back to, no matter what in the script that I try to change.
		end
	end
	Active.Value = Active.Value+1
	TweenService:Create(gui.notification, TweenInf, {Position = UDim2.new(1,0,0.9,0)}):Play()
	workspace.GlobalAudios.Misc.Notification:Play()
	gui.notification.notificationlabel.Text = label
	gui.notification.contents.notificationtext.Text = text
	task.wait(8)
	print(notificationguis)
	TweenService:Create(gui.notification, TweenInf2, {Position = UDim2.new(1.35,0,gui.notification.Position.Y.Scale,0)}):Play()
	task.wait(0.5)
	gui:Destroy()
	gui = nil
	table.remove(notificationguis,Active.Value)
	Active.Value = Active.Value-1
end)

Line 41, the line the error always falls back to, has been labeled.

How can I prevent this error from reoccurring when this system goes live in my game’s next update?

this issue is from how you’re handing queuing and dequeueing

e.g. working case
first notification
screenGui1 is added to the table
all guis are tweened upwards 
Active=1
wait 9 seconds
screenGui1 is destroyed
item in table at [Active] is removed
✅ that works normally
e.g. failure case
first notification
screenGui1 is added to the table
all guis are tweened upwards
Active=1
wait 4 seconds
second notification
screenGui2 is added to the table
all guis are tweened upwards
Active=2
wait 4 seconds
screenGui1 is destroyed
Active==2, so we remove the screenGui2 ❎
   **this is an issue. you remove the wrong one**
wait 2 seconds
screenGui3 is added to the table
all guis are tweened upwards
   the for loop sees screenGui1 and screenGui3 in the table
   screenGui1 was destroyed and its children aren't its children anymore
      **error is thrown here**

remove notificationguis[1] instead of notificationguis[Active]

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