Level-up pop up getting bombarded

I’ve been trying to make a level-up pop up but to no avail. It seems that the effects event gets spammed somehow and I don’t know how to fix it. Does anyone have any ideas on how to make them play one by one and properly?

EffectsHandler:

local TweenService = game:GetService("TweenService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Events = ReplicatedStorage:FindFirstChild("Events")
local Effects = Events:FindFirstChild("Effects")

local que = 0
local levels = {}
local busy = false

Effects.Event:Connect(function(Player, CurrentLevel, NewLevel)
	local PlayerGui = Player.PlayerGui
	local GameGui = PlayerGui.GameGui
	local MainFrame = GameGui.MainFrame
	local LevelFrame = MainFrame.LevelFrame
	local LevelLabel = LevelFrame.LevelLabel
	
	if que > 1 then
		for _, order in pairs(levels) do
			if order.OldLevel ~= CurrentLevel then
				que += 1
				levels[que] = {["OldLevel"] = CurrentLevel, ["Level"] = NewLevel}
			end
		end
	else
		que += 1
		levels[que] = {["OldLevel"] = CurrentLevel, ["Level"] = NewLevel}
	end
	
	while task.wait() do
		
		print(que)

		if busy == false and levels[1] then
			busy = true

			local infoDown = TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0)
			local tweenDown = TweenService:Create(LevelFrame, infoDown, {Position = UDim2.new(0.5, 0, 0.1, 0)})
			LevelLabel.Text = levels[1].OldLevel
			LevelFrame.Visible = true

			tweenDown:Play()

			tweenDown.Completed:Connect(function()

				local infoIn = TweenInfo.new(0.5, Enum.EasingStyle.Quart, Enum.EasingDirection.In, 0, false, 0)
				local tweenIn = TweenService:Create(LevelFrame, infoDown, {Size = UDim2.new(0.03, 0, 0.067, 0)})
				tweenIn:Play()

				tweenIn.Completed:Connect(function()

					local infoOut = TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false, 0)
					local tweenOut = TweenService:Create(LevelFrame, infoDown, {Size = UDim2.new(0.042, 0, 0.08, 0)})
					tweenOut:Play()

					tweenOut.Completed:Connect(function()

						local LevelUp = script.LevelUp:Clone()
						LevelUp.Parent = game.StarterGui
						LevelUp:Play()

						LevelLabel.Text = levels[1].Level

						local infoNormal = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.In, 0, false, 0)
						local tweenNormal = TweenService:Create(LevelFrame, infoDown, {Size = UDim2.new(0.037, 0, 0.075, 0)})
						tweenNormal:Play()

						tweenNormal.Completed:Connect(function()

							LevelFrame.Visible = false
							LevelFrame.Position = UDim2.new(0.5, 0, -0.1, 0)
							table.remove(levels, 1)
							busy = false
							que -= 1

						end)

					end)

				end)

			end)
		end	
	end
	
end)

ValueHandler:

local TweenService = game:GetService("TweenService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Events = ReplicatedStorage:FindFirstChild("Events")
local Effects = Events:FindFirstChild("Effects")

local dataManager = require(ServerScriptService:WaitForChild("Modules"):WaitForChild("DataManager"))

function ChangeExp(Player)
	local Leaderstats = Player:FindFirstChild("Leaderstats")
	local Exp = Leaderstats:FindFirstChild("Exp")
	local MaxExp = Leaderstats:FindFirstChild("MaxExp")
	local Level = Leaderstats:FindFirstChild("Level")
	local SP = Leaderstats:FindFirstChild("SP")
	
	local profile = dataManager.Profiles[Player]
	if not profile then return end
	
	if Exp.Value > MaxExp.Value then
		
		Exp.Value -= MaxExp.Value
		profile.Data.Exp = Exp.Value
		
		profile.Data.Level += 1
		Level.Value = profile.Data.Level
		
		profile.Data.SP += 3
		SP.Value = profile.Data.SP
		
		profile.Data.MaxExp = math.round(MaxExp.Value+5+(MaxExp.Value/15))
		MaxExp.Value = profile.Data.MaxExp
		
		ChangeExp(Player)
		
	elseif Exp.Value == MaxExp.Value then
		
		Exp.Value -= MaxExp.Value
		profile.Data.Exp = Exp.Value

		profile.Data.Level += 1
		Level.Value = profile.Data.Level
		
		profile.Data.SP += 3
		SP.Value = profile.Data.SP

		profile.Data.MaxExp = math.round(MaxExp.Value+5+(MaxExp.Value/15))
		MaxExp.Value = profile.Data.MaxExp
	end
	
	Effects:Fire(Player, (Level.Value - 1), Level.Value)
	
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Leaderstats = Player:WaitForChild("Leaderstats")
		local Exp = Leaderstats:WaitForChild("Exp")
		local Chest = Leaderstats:WaitForChild("Chest")
		local Weapon = Leaderstats:WaitForChild("Weapon")
		local Level = Leaderstats:WaitForChild("Level")
		
		Exp.Changed:Connect(function()
			local CurrentLevel = Level.Value
			
			ChangeExp(Player)
			
			if Level.Value > CurrentLevel then
				
			end
		end)
		
		Chest.Changed:Connect(function()
			ChangeChest(Player)
		end)
		
		Weapon.Changed:Connect(function()
			ChangeWeapon(Player)
		end)
	end)
end)

https://gyazo.com/558608b0061d534dedddfec186684c83

1 Like

a new loop is made every time the connection is fired, with 0 condition to stop it. This is bound to result in a memory leak, or the issue you have here that’s occurred.

There’s no need to keep track of the que since levels in an array, and you can determine it based off the number of values.

If the issue you’re directing is them all playing seemingly at the same time, then consider using the :Wait() method instead of creating a rbxscriptconnection, this will yield the thread until the event is called, meaning no code after the method’s calling will run until.

2 Likes

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