Why does my UI only tween once

I’m trying to make it so if you touch a part a UI tweens in and out of the screen. It works the first time, but if I try and do it again it doesn’t work. Could someone please point out the issue?

local Part1 = workspace.Stage1Door.touchPart
local Part2 = workspace.Stage2Door.touchPart
local Part3 = workspace.Stage3Door.touchPart
local Part4 = workspace.Stage4Door.touchPart
local Sound = game.SoundService.Success
local SoundError = game.SoundService.Error
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local DB = false
local player = game.Players.LocalPlayer

local mainValue = game.ReplicatedStorage.StageValuesFolder.Stage1
local mainValue2 = game.ReplicatedStorage.StageValuesFolder.Stage2
local mainValue3 = game.ReplicatedStorage.StageValuesFolder.Stage3
local mainValue4 = game.ReplicatedStorage.StageValuesFolder.Stage4
local mainStats = require(game.ReplicatedStorage.stageValues)

Part1.Touched:Connect(function(Hit)
	if not DB and Character == Hit.Parent and player.leaderstats.Cash.Value >= mainStats.StageCosts.Stage1.Cost and player.leaderstats.Level.Value >= mainStats.StageCosts.Stage1.Level and mainValue.Value == false then 
		DB = true
		Sound:Play()
		player.PlayerGui.Alert.AlertText.Text = "Stage 1 Unlocked!"
		player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(34, 255, 0)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
		Sound.Ended:Wait()
		wait(1)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
		DB = false
	elseif player.leaderstats.Cash.Value < mainStats.StageCosts.Stage1.Cost or player.leaderstats.Level.Value < mainStats.StageCosts.Stage1.Level then
		if not DB and Character == Hit.Parent and SoundError.IsPlaying == false then
			DB = true
			SoundError:Play()
			player.PlayerGui.Alert.AlertText.Text = "Not enough!"
			player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(255, 0, 0)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
			wait(3)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
			DB = false
		end
	end
end)

Part2.Touched:Connect(function(Hit)
	if not DB and Character == Hit.Parent and player.leaderstats.Cash.Value >= mainStats.StageCosts.Stage2.Cost and player.leaderstats.Level.Value >= mainStats.StageCosts.Stage2.Level and mainValue2.Value == false then 
		DB = true
		Sound:Play()
		player.PlayerGui.Alert.AlertText.Text = "Stage 2 Unlocked!"
		player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(34, 255, 0)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
		wait(1)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
		DB = false
	elseif player.leaderstats.Cash.Value < mainStats.StageCosts.Stage2.Cost or player.leaderstats.Level.Value < mainStats.StageCosts.Stage2.Level then
		if not DB and Character == Hit.Parent and SoundError.IsPlaying == false then
			DB = true
			SoundError:Play()
			player.PlayerGui.Alert.AlertText.Text = "Not enough!"
			player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(255, 0, 0)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
			wait(3)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
			DB = false
		end
	end
end)

Part3.Touched:Connect(function(Hit)
	if not DB and Character == Hit.Parent and player.leaderstats.Cash.Value >= mainStats.StageCosts.Stage3.Cost and player.leaderstats.Level.Value >= mainStats.StageCosts.Stage3.Level and mainValue3.Value == false then 
		DB = true
		Sound:Play()
		player.PlayerGui.Alert.AlertText.Text = "Stage 3 Unlocked!"
		player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(34, 255, 0)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
		wait(1)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
		DB = false
	elseif player.leaderstats.Cash.Value < mainStats.StageCosts.Stage3.Cost or player.leaderstats.Level.Value < mainStats.StageCosts.Stage3.Level then
		if not DB and Character == Hit.Parent and SoundError.IsPlaying == false then
			DB = true
			SoundError:Play()
			player.PlayerGui.Alert.AlertText.Text = "Not enough!"
			player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(255, 0, 0)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
			wait(3)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
			DB = false
		end
	end
end)

Part4.Touched:Connect(function(Hit)
	if not DB and Character == Hit.Parent and player.leaderstats.Cash.Value >= mainStats.StageCosts.Stage4.Cost and player.leaderstats.Level.Value >= mainStats.StageCosts.Stage4.Level and mainValue4.Value == false then 
		DB = true
		Sound:Play()
		player.PlayerGui.Alert.AlertText.Text = "Stage 4 Unlocked!"
		player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(34, 255, 0)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
		wait(1)
		player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
		DB = false
	elseif player.leaderstats.Cash.Value < mainStats.StageCosts.Stage4.Cost or player.leaderstats.Level.Value < mainStats.StageCosts.Stage4.Level then
		if not DB and Character == Hit.Parent and SoundError.IsPlaying == false then
			DB = true
			SoundError:Play()
			player.PlayerGui.Alert.AlertText.Text = "Not enough!"
			player.PlayerGui.Alert.AlertText.TextColor3 = Color3.fromRGB(255, 0, 0)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,0, 0),"In","Sine",0.75)
			wait(3)
			player.PlayerGui.Alert.AlertText:TweenPosition(UDim2.new(0.438, 0,-1, 1),"Out","Sine",0.75)
			DB = false
		end
	end
end)```
1 Like

Might I ask why there are “4 stages” in a script and not in separate scripts?

If you want, you can name the scripts, put them into a folder so they don’t get lost in the game, and so it’s organized.

but for the tween service, try writing the script inside another GUI, test to see whats wrong so you don’t have to go through hundreds of code just so you can fix one error that’s inside all of the stage lines.

It’s got nothing to do with how organized the scripts are