I made a timing bar and encountered some problems (Function fires multiple times despite excuting once)

  1. What do you want to achieve?
    I was working on a timing bar script that involves clicking a slime then a ‘Catch’ button will pop up, along with a timing bar. Pressing ‘Catch’ when the bar arrives on the green part will cause the number on the catch button to go down then you will have to replay the timing minigame again. Once the number goes to 0 you fully caught the slime. I used a Tween to make the values go smoothly and so that the value and the position of the bar matches. When pressing ‘Catch’, the Tween will pause and the script will check the values of the bar to make sure it is inside of the green part. I also made the green part change spots randomly every successful catch or every time you re-catch.

  2. What is the issue?
    The issue is that when you miss the green part and clicked the ‘Catch’ button on the red part, The CatchButton.MouseButton1Click function somehow runs twice and if you miss again it runs 3 times. The script actually works fine if you don’t miss.
    Another problem is that there are multiple slimes, if you missclicked on the first slime and clicked the second slime, The values of the green bar carries on to the second slime and again, the function fires twice (The function of the first slime and the second slime)

  3. What solutions have you tried so far?
    I tried changing the script into a localscript and used a remote event but the same problem still happens, if you miss the green part and clicked the red part, the function runs twice. I tried looking for solutions on the forum and what I’ve found is to add debounce but the problem still persists.

Here’s the part of the script that is the problem, I’ve removed some of the parts that don’t really affect the script since they’re just Tweens that are for deco only.
** Note : The bar bounces back when it reaches the edge

local db = false
local catchValue = 3
local slime = script.Parent
SlimeCatchingGUI.CatchButton.MouseButton1Click:Connect(function()
	if db == false then
		db = true
		numSlideTween:Pause() -- Pausing the Tween to check the Values
		slideTween:Pause()
			if catchValue > 1 then 
				if bar.Value.Value <= startPoint+offsetValue and bar.Value.Value >= startPoint then
					catchValue += -1
-- There are effects here showing  you caught the slime
					wait(1)
					bar.Value.Value = 0
					bar.Position = UDim2.new(0,0,0,0) -- Resetting the bar's position back to 0
					startPoint = math.random(15,85)/100 -- Generating a new start point and offset value for the green part
					offsetValue = math.random(20,25)/100
					SlimeCatchingGUI.Frame.Redbar.Greenbar.Position = UDim2.new(startPoint,0,0,0)
					SlimeCatchingGUI.Frame.Redbar.Greenbar.Size = UDim2.new(offsetValue,0,1,0)
					slideTween:Play() -- Playing the bar tween
					numSlideTween:Play()
					db = false
				else
					SlimeCatchingGUI.Enabled = false -- If you miss, the whole timing bar GUI closes
					db = false
				end
			else -- You've caught the slime 2 times and this is the last time you have to catch it
				if bar.Value.Value < startPoint+offsetValue and bar.Value.Value > startPoint then
-- There are effects here showing  you caught the slime
					wait(1)
					SlimeCatchingGUI.Enabled = false
					player.slimestats["Slime"].Value += 1 -- You gain a stat when you caught the slime
					slime:Destroy() -- Slime disappears, deleting the script in the process
					db = false
				else
					SlimeCatchingGUI.Enabled = false 
					db = false
				end
			end
		end
	end)

I’m relatively new to roblox scripting and this problem is super confusing to explain. If anyone doesn’t understand and wants the whole script, Let me know.

If the same ui is not used for different slimes, you could replace :connect with :once

It still makes the function run twice if you missed the green part :sob:
and now you can’t re-click the same slime to try capturing it again