Tween Plays twice instead once

I’ve been trying to make a notification appear whenever you level up or reach a new milestone. Whenever the player levels up or reaches a new milestone it does appear and plays the tween that makes it disappear but whenever it disappears it becomes visible again and replays the tween. I don’t know how to fix this bug, so any help will be very appreciated.

Here is what the bug looks like

location:
image

Script:

local TweenService = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local Level = Player.PlayerInfo.Levels.Level

local Milestones = {
	["Iron Pickaxe"] = 7,
	["Gold Pickaxe"] = 12,
}

game.Workspace.ExpPart.ClickDetector.MouseClick:Connect(function()
	for milestone, value in pairs(Milestones) do	
		if Level.Value == value then
			local Template = game:GetService("ReplicatedStorage").Templates.Levels:Clone()
			Template.Text = milestone.." Unlocked"
			Template.Parent = script.Parent
			
			print(milestone.." Unlocked , Level "..value.."+")
			
			TweenService:Create(Template, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Position = UDim2.new(0.823, 0, 0.79, 0), TextTransparency = 1, TextStrokeTransparency = 1}):Play()
			wait(3)
			Template:Destroy()
			
		else
			local Template = game:GetService("ReplicatedStorage").Templates.Levels:Clone()
			Template.Text = "Level up!"
			Template.Parent = script.Parent
			
			print("Level up!")

			TweenService:Create(Template, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Position = UDim2.new(0.823, 0, 0.79, 0), TextTransparency = 1, TextStrokeTransparency = 1}):Play()
			wait(3)
			Template:Destroy()
		end
	end
end)

1 Like

It plays twice because you have a loop that runs when you click the click detector. If you add another value to the Milstones table, it will run 3 times. If you don’t want the tween to be played twice but still keep the loop, add an if statement to your tween like this:

if not alreadyPlayed then -- Make a new variable called alreadyPlayed
    alreadyPlayed = true
    TweenService:Create(Template, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Position = UDim2.new(0.823, 0, 0.79, 0), TextTransparency = 1, TextStrokeTransparency = 1}):Play()
	wait(3)
	Template:Destroy()
end

The problem is I want it to notify still whenever the player joins so if they level up twice at once I want it to show 2 labels instead of 4. Adding the value you recommended will prevent that.

maybe if i add it to the end and immediately make it false it may work

Yep, if you make the value false again when the loop finishes, that might do the job.