Debounce issues

I’m having an issue with debounce for my menu UI, essentially the problem is that the audio (the click sound) will play multiple times when my mouse hovers over the part, rather than once.

Everything else works fine, it’s just that the audio will repeatedly play.

I’ve tried setting the debounce at different points of the code (such as after the tween plays and such) but it doesn’t seem to stop the issue.

The shortest video shows what the audio should sound like vs what it sounds like in the game.

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse()

local button_part = game.Workspace:WaitForChild("Shop")
local button_clone = button_part:WaitForChild("Clone") --Tweens the surface gui part's position to a transparent clone in front for the mouse hover animation
local button_Position = button_part.Position

local tween_service = game:GetService("TweenService")
local tween_info = TweenInfo.new(0.8, Enum.EasingStyle.Elastic)

mouse.TargetFilter = button_clone
local debounce = nil

mouse.Move:Connect(function()
	if mouse.Target == button_part then
		if not debounce then
			script.Click:Play()
			script.Click.Ended:Connect(function()
				debounce = false
			end)
		end
		local tween = tween_service:Create(button_part, tween_info, {Position = button_clone.Position})
		tween:Play()
		script.Parent.TextButton.UIGradient.Enabled = true
		script.Parent.TextButton.UIStroke.Enabled = true
	else
		if debounce then 
			debounce = false
		end
		local tween = tween_service:Create(button_part, tween_info, {Position = button_Position})
		tween:Play()
		tween.Completed:Connect(function()
			script.Parent.TextButton.UIGradient.Enabled = false
			script.Parent.TextButton.UIStroke.Enabled = false
		end)
	end
end)

Instead of this

script.Click.Ended:Connect(function()
	debounce = false
end)

Try this

script.Click.Ended:Wait()
debounce = false

You never made the debounce equal to true.
“if not” activates if the value given is false or nil so you would have to make debounce equal to true here

if not debounce then
    debounce = true
	script.Click:Play()
    script.Click.Ended:Wait()
    debounce = false

I don’t understand? If your wanting to play a sound when the users mouse enters the button, just do.

button.MouseEnter:Connect(function()
 sound:Play()
end)

I should’ve considered that, I was having issues with the MouseEnter event for tweeting parts so I ditched it entirely, thank you for the reply.

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