How can I add debounce to this

How can I add debounce/something else to my script in order to stop the button getting bigger but the text remaining the same size:

Code:

local ogSize = script.Parent.Size
local duration = 0.2
local button = script.Parent
local text = button.Text
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(button, tweenInfo,{TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo,{TextSize = 40})

script.Parent.MouseEnter:Connect(function()
	button:TweenSize(ogSize + UDim2.new(0.1,0.1,0.1,0.1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
	tween1:Play()
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent:TweenSize(ogSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
	tween2:Play()
end)
1 Like

local debounce = false

then in functions at the start check if debounce is false, if it is then blablabla

at the end of the function do debounce = true, then put a task.wait( time ) and then debounce = false

porblem fixed dawg

2 Likes

You’re supposed to stop one tween first when playing the other one

On MouseEnter -
Tween2:Stop()
Tween1:Play()

On MouseLeave -
Tween1:Stop()
Tween2:Play()

(The method above is to avoid tween overlap. Debounce may still be needed, the way shimejik has defined

3 Likes

You have two tweens one for when the mouse enters an area and one when it leaves … So where would you want to debounce this. From the video it looks like you’re looking to make sure the animation works as intended. So …

local ogSize = script.Parent.Size
local duration = 0.2
local button = script.Parent
local text = button.Text
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(button, tweenInfo,{TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo,{TextSize = 40})
local db = true

script.Parent.MouseEnter:Connect(function()
	if db then db = false
		button:TweenSize(ogSize + UDim2.new(0.1,0.1,0.1,0.1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
		tween1:Play()
	end
end)

script.Parent.MouseLeave:Connect(function()
	if db == false then db = true
		script.Parent:TweenSize(ogSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
		tween2:Play()
	end
end)

This runs the 1st when they enter and runs the 2nd when they leave, blocking all other outcomes.
It’s a double debounce and a toggle at the same time.

I see no reason this wouldn’t work … did you even try it.

2 Likes

Havent programmed lua in over a year so, bear with me…

local ogSize = script.Parent.Size
local duration = 0.2
local button = script.Parent
local text = button.Text
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(button, tweenInfo, {TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo, {TextSize = 40})

local debounce = false

script.Parent.MouseEnter:Connect(function()
    if not debounce then
        debounce = true
        button:TweenSize(ogSize + UDim2.new(0.1, 0.1, 0.1, 0.1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
        tween1:Play()
        wait(duration)  -- Wait for the animation to finish before allowing another mouse event
        debounce = false
    end
end)

script.Parent.MouseLeave:Connect(function()
    if not debounce then
        debounce = true
        script.Parent:TweenSize(ogSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
        tween2:Play()
        wait(duration)  -- Wait for the animation to finish before allowing another mouse event
        debounce = false
    end
end)
1 Like

Ive noticed that in all the ways ive tried here, (everyone’s suggestions), that it still continues to either get stuck as big, but no longer the text remains enlarged. Occasionally, when the mouse leaves the button, the GUI remains enlarged.

maybe you could do something where you wait for the mouse to leave in the same function as the mouse enter (sorry for bad explanation will try to show example)

1 Like

I just tried that as:

script.Parent.MouseEnter:Connect(function()
	if not debounce then
		debounce = true
		button:TweenSize(ogSize + UDim2.new(0.1, 0.1, 0.1, 0.1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
		tween1:Play()
		wait(duration)  -- Wait for the animation to finish before allowing another mouse event
		debounce = false
		script.Parent.MouseLeave:Connect(function()
			if not debounce then
				debounce = true
				script.Parent:TweenSize(ogSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration)
				tween2:Play()
				wait(duration)  -- Wait for the animation to finish before allowing another mouse event
				debounce = false
			end
		end)
	end
end)

Which is what you meant?

yeah except I’m not sure if the function(inside the other function) will fire no matter what

1 Like
local ogSize = script.Parent.Size
local duration = 0.2
local button = script.Parent
local text = button.Text
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(button, tweenInfo,{TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo,{TextSize = 40})

local mouseInGui = false

script.Parent.MouseEnter:Connect(function()
	button.Size = 40
	tween1:Play()
end)

script.Parent.MouseLeave:Connect(function()
	button.Size = 60
	tween2:Play()
end)

This probably won’t work but idk

Well, it doesn’t work because to change the button’s size you need to use UDim2.new()

You can’t change the button without changing text size without making some sort of algorithm for calculating the exact ratio the text si- I’m not even going to bother explaining this, it’s way too much work for what you want.

Why don’t you just make a textLabel over the button, where the text is the exact size? With transparent backgroundand clickthrough enabled. Then if you want the text to change (for example to change color) when you hover over the button you can just do something like

button.MouseEnter:Connect(function()
  TextLabel.TextColor = Color3.new(0,0,0)
end)

button.MouseLeave:Connect(function()
  TextLabel.TextColor = Color3.new(255,255,255)
end)

Of course you want to use different colors for this

1 Like

I would do that honestly, and I have before, but I have seen it in numerous other games and so I wish to achieve that lmao.
Its pretty close, I just think that MouseEntered/MouseLeave is running too slowly and so I will have to find another way.

local ogSize = script.Parent.Size
local button = script.Parent
local duration = 0.2

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

local tween1 = TweenService:Create(button, tweenInfo, {Size = ogSize + UDim2.new(0.1,0.1,0.1,0.1), TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo, {Size = ogSize, TextSize = 40})

script.Parent.MouseEnter:Connect(function()
	tween1:Play()
end)

script.Parent.MouseLeave:Connect(function()
	tween2:Play()
end)
1 Like

Turns out, you had to set the “Override” parameter to true (In the TweenSize function). Turned out fine when I tested it.

local ogSize = script.Parent.Size
local duration = 0.2
local button = script.Parent
local text = button.Text
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(button, tweenInfo,{TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo,{TextSize = 40})

script.Parent.MouseEnter:Connect(function()
	button:TweenSize(ogSize + UDim2.new(0.1,0.1,0.1,0.1), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration, true)
	tween1:Play()
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent:TweenSize(ogSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, duration, true)
	tween2:Play()
end)
1 Like

What happens is the TextSize no longer enlarges/minimizes.

Is it fixed? Otherwise you can send me the gui and I’ll fix it.

1 Like

I found help on a forum on Discord and I found that removing my UI size constraint allowed me to fix the problem.

local ogSize = script.Parent.Size
local button = script.Parent
local duration = 0.2

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

local tween1 = TweenService:Create(button, tweenInfo, {Size = ogSize + UDim2.new(0.1,0.1,0.1,0.1), TextSize = 60})
local tween2 = TweenService:Create(button, tweenInfo, {Size = ogSize, TextSize = 40})

script.Parent.MouseEnter:Connect(function()
	if button.MouseEnter then
		print("mouse entered")
	end
	tween1:Play()
end)

script.Parent.MouseLeave:Connect(function()
	if button.MouseLeave then
		print("mouse left")
	end
	tween2:Play()
end)

Code for anyone with issues, thank you to everyone who helped me ( sorry I cant give all yall the solution check :frowning: )

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