Cooldown on UI tweening not working?

  1. What do you want to achieve? I want to make a cooldown for my GUI Open/Close tweening to prevent it from being bugged by spamming.

  2. What is the issue? The cooldowns work normally, but when I spam the button to Open/Close the UI, it seems to somehow bypass the cooldowns??

  3. What solutions have you tried so far? I’ve searched around the web and Dev Forum, and no posts have been made on this.

Code:

frame = script.Parent.Parent:WaitForChild("BackGround")

cooldown = false

script.Parent.MouseButton1Click:Connect(function()
	if frame.Visible == false and cooldown == false then
		cooldown = true
		frame.Visible = true
		frame:TweenPosition(UDim2.new(.315, 0,0.147, 0), "Out", "Elastic", 1.5, false)
		wait(3)
		cooldown = false
	else
		cooldown = true
		frame:TweenPosition(UDim2.new(-0.5, 0,0.147, 0), "Out", "Quad", 1, false)
		wait(1)
		frame.Visible = false
		wait(4)
		cooldown = false
	end
end)

I’m also aware that :TweenPosition is not as reliable as TweenService, but it’s there for convenience.

2 Likes

you’re making the cooldown value as it was a ‘IsOpened’ value, i suggest doing it like this;

frame = script.Parent.Parent:WaitForChild("BackGround")

isOpened = false
cooldown = false

script.Parent.MouseButton1Click:Connect(function()
	if cooldown == true then return end -- If it's still tweening open/close

	if frame.Visible == false and isOpened == false then -- Open
		isOpened = true
		cooldown = true
		
		frame.Visible = true
		frame:TweenPosition(UDim2.new(.315, 0,0.147, 0), "Out", "Elastic", 1.5, false)
		task.wait(3)
		cooldown = false
	else -- Close
		isOpened = false
		cooldown = true

		frame:TweenPosition(UDim2.new(-0.5, 0,0.147, 0), "Out", "Quad", 1, false)
		task.wait(1)
		frame.Visible = false
		task.wait(4)
		cooldown = false
	end
end)

A bit messy code.

1 Like

The only reason I was making it invisible was due to it not working for some reason before, but I’ll try that out. Looks like it SHOULD work-

Yup, it worked! Thanks for the advice! I forgot return end was a thing heh-

1 Like

No problem!, you should try to use TweenService like this;

local tweenSer = game:GetService("TweenService")

local positions = { -- Target Positions you can modify
	Open = UDim2.new(.315, 0,0.147, 0);
	Close = UDim2.new(-0.5, 0,0.147, 0)
}

local tweenInfos = { -- Tween Info you can modify
	Open = TweenInfo.new(1.5, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out);
	Close = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
}

local frame = -- frame

local isOpened = true

local t -- Tween connection

script.Parent.MouseButton1Click:Connect(function()
	if t ~= nil then return end -- If tween connection still running
	
	if frame.Visible == false and isOpened == false then -- Open
		
		isOpened = true

		frame.Visible = true
		
		t = tweenSer:Create(frame, tweenInfos.Open, {Position = positions.Open}) -- Create tween
		t:Play() -- Play tween
		
		t.Completed:Wait() -- Waits until tween is done.
		t = nil
		
	else -- Close
		
		isOpened = false
		
		t = tweenSer:Create(frame, tweenInfos.Close, {Position = positions.Close})
		t:Play()
		
		t.Completed:Wait() -- Waits until tween is done.
		t = nil
		
		frame.Visible = false
		
	end
end)

it’s a bit more cleaner

1 Like

Mhm, but I mentioned at the bottom of my post that it was for convenience, but I’ll be sure to use swap it to TweenService when I have time. But anyway, thanks for the help, I greatly appreciate it.

1 Like