Using TweenService to play a button animation freezes the script?

I’m trying to play a tween animation for when the button is clicked. However when I click the button the first time, everything works but the second time nothing happens and the button stays red. No errors or anything. Any idea what’s going on?

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.25)
local goal1 = {Size = Vector3.new(1, 1, 0.05)}
local goal2 = {Size = Vector3.new(1, 1, 0.25)}
local Enabled = script.Parent.Parent.Enabled.Value
local Lasers = script.Parent.Parent.Lasers
local Button = script.Parent
local tween1 = tweenService:Create(Button, tweenInfo, goal1)
local tween2 = tweenService:Create(Button, tweenInfo, goal2)

debounce = false
function wat(player)
	if player.TeamColor == script.Parent.Parent.Parent.Parent.TeamColor.Value then
		if Enabled and not debounce then
			debounce = true
			Enabled = false
			tween1:Play()
			Button.BrickColor = BrickColor.new("Really red")
			Lasers.BrickColor = BrickColor.new("Bright green")
			Lasers.Transparency = 0.8
			Lasers.A.Enabled = false
			Lasers.B.Enabled = false
			Lasers.C.Enabled = false
			wait(0.5)
			tween2:Play()
			debounce = false
		end	
	elseif not Enabled and not debounce then
		debounce = true
		Enabled = true
		tween1:Play()
		Button.BrickColor = BrickColor.new("Bright green")
		Lasers.BrickColor = BrickColor.new("Really red")
		Lasers.Transparency = 0.5
		Lasers.A.Enabled = true
		Lasers.B.Enabled = true
		Lasers.C.Enabled = true
		wait(0.5)
		tween2:Play()
		debounce = false
	end
end
script.Parent.ClickDetector.MouseClick:connect(wat)

tween1 makes the button push in, and tween2 makes the button push back out to simulate a pressing button animation.

The elseif is in the wrong place, it is connected to the player.teamcolor “if” statement. You can move the end that is before the elseif to the end of the function

1 Like