My debounce is working but it is very glitchy

I am trying to make a bar that appears and fills up when G is held down, pauses when G is released, waits a bit, then disappears, however, the debounce I added seem to only work when I don’t spam G

local UserInputService = game:GetService("UserInputService")
local db4 = false
local IsShooting = false

local Scroll = script.Parent.Scroll
local TweenService = game:GetService("TweenService")
local FillTween = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 244)})
local FillTween2 = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.486, 0)})

local FillTweenReset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 1)})
local FillTween2Reset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.678, 0)})
-- In a local script
game:GetService("UserInputService").InputBegan:connect(function(input) 
	
	
	if input.KeyCode == Enum.KeyCode.G and db4 == true then return end 
	if input.KeyCode == Enum.KeyCode.G and db4 == false  
	then 
		print("bar is active")
		db4 = true
		IsShooting = true
		if IsShooting == true then
			script.Parent.Enabled = true
		FillTween:Play()
				FillTween2:Play()
				end
end
game:GetService("UserInputService").InputEnded:connect(function(input)
	
		IsShooting = false
		
			if IsShooting == false then
				FillTween:Pause()
				FillTween2:Pause()
		wait(3)
				script.Parent.Enabled = false
				
				FillTween:Cancel()
				FillTween2:Cancel()
			end
			wait(1)
			FillTweenReset:Play()
		FillTween2Reset:Play()
		wait(2)
			db4 = false
end)





end)




	


how can I fix this script so that the debounce works too when I spam G?

I have attached a video in case anyone is wondering what my problem is.

also i forgot to mention that db4 is the debounce variable

Try this:

local UserInputService = game:GetService("UserInputService")
local db4 = false
local IsShooting = false

local Scroll = script.Parent.Scroll
local TweenService = game:GetService("TweenService")
local FillTween = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 244)})
local FillTween2 = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.486, 0)})

local FillTweenReset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 1)})
local FillTween2Reset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.678, 0)})
-- In a local script

game:GetService("UserInputService").InputBegan:connect(function(input,gmp)
	if gmp then -- If Player Chatting.
		return
	end
	if db4 then -- If debounce == true
		return
	end
	if input.KeyCode == Enum.KeyCode.G and db4 == false then 
		print("bar is active")
		db4 = true
		IsShooting = true
	end
	if IsShooting == true then
		script.Parent.Enabled = true
		FillTween:Play()
		FillTween2:Play()
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(input,gmp)
	if gmp then -- If PlayerChatted
		return
	end
	IsShooting = false
		
	if IsShooting == false then
		FillTween:Pause()
		FillTween2:Pause()
		task.wait(3)
		script.Parent.Enabled = false
		FillTween:Cancel()
		FillTween2:Cancel()
	end
	task.wait(1)
	FillTweenReset:Play()
	FillTween2Reset:Play()
	task.wait(2)
	db4 = false
end)

Tell me if there’s any errors : )

You just have to analyze the process:

  • When InputBegan fires the db4 becomes true and when the InputEnded fires it sets the db4 = false after 2 seconds.

BUT what’s happening is:

  • When InputBegan fires the db4 becomes true and when the InputEnded fires it sets the db4 = false
  • When InputBegan fires again… before 2 seconds it returns because dv4 = true but the InputEnded still fires and and still waits 2 again seconds before db4 = false.

What you can do is:
add a if statement return on InputEnded the same as the InputBegan

UIS.inputEnded:Connect(function(input)
if db4 == true then return end

So it prevents from firing again and had to wait 2 seconds. Hope this helps.

edit:
One thing to note it is more accurate to use the new task library.

– Use
task.wait(2) // More faster and Accurate
– Instead of
wait(2)

local UserInputService = game:GetService("UserInputService")
local db4 = false
local IsShooting = false

local Scroll = script.Parent.Scroll
local TweenService = game:GetService("TweenService")
local FillTween = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 244)})
local FillTween2 = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.486, 0)})

local FillTweenReset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 1)})
local FillTween2Reset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.678, 0)})
-- In a local script

game:GetService("UserInputService").InputBegan:connect(function(input,gmp)
	if gmp then -- If Player Chatting.
		return
	end
	if db4 == true then -- If debounce == true  -- changes here
		return
	end
	if input.KeyCode == Enum.KeyCode.G and db4 == false then 
		print("bar is active")
		db4 = true
		IsShooting = true
	end
	if IsShooting == true then
		script.Parent.Enabled = true
		FillTween:Play()
		FillTween2:Play()
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(input,gmp)
	if db4 == false then return end
	if gmp then -- If PlayerChatted
		return
	end
	IsShooting = false

	if IsShooting == false and db4 == true then -- changes here
		FillTween:Pause()
		FillTween2:Pause()
		task.wait(3)
		script.Parent.Enabled = false
		FillTween:Cancel()
		FillTween2:Cancel()
	end
	task.wait(1)
	FillTweenReset:Play()
	FillTween2Reset:Play()
	task.wait(2)
	db4 = false
end)

The changes I made are labeled with – changes here. The bar is still glitchy when G is spammed

Here made some changes, try this:

local UserInputService = game:GetService("UserInputService")
local db4 = false
local IsShooting = false

local Scroll = script.Parent.Scroll
local TweenService = game:GetService("TweenService")
local FillTween = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 244)})
local FillTween2 = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.486, 0)})

local FillTweenReset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 1)})
local FillTween2Reset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.678, 0)})
-- In a local script

game:GetService("UserInputService").InputBegan:connect(function(input,gmp)
	if gmp then -- If Player Chatting.
		return
	end
	if db4 == true then -- If debounce == true  -- changes here
		return
	end
	if input.KeyCode == Enum.KeyCode.G and db4 == false then 
		print("bar is active")
		db4 = true
		IsShooting = true
		script.Parent.Enabled = true --
		FillTween:Play()
		FillTween2:Play()
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(input,gmp)
	if db4 == false then return end
	if gmp then -- If PlayerChatted
		return
	end
	if input.KeyCode == Enum.KeyCode.G then 
		IsShooting = false
		FillTween:Pause()
		FillTween2:Pause()
		task.wait(3)
		script.Parent.Enabled = false
		FillTween:Cancel()
		FillTween2:Cancel()
		task.wait(1)
		FillTweenReset:Play()
		FillTween2Reset:Play()
		task.wait(2)
		db4 = false
	end
end)

Found out there are some errors to the scripts above, I edited it

local UserInputService = game:GetService("UserInputService")
local db4 = false
local IsShooting = false

local Scroll = script.Parent.Scroll
local TweenService = game:GetService("TweenService")
local FillTween = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 244)})
local FillTween2 = TweenService:Create(Scroll, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.486, 0)})

local FillTweenReset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Size = UDim2.new(0, 23,0, 1)})
local FillTween2Reset = TweenService:Create(Scroll, TweenInfo.new(0.001, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{Position = UDim2.new(0.104, 0,0.678, 0)})
-- In a local script
game:GetService("UserInputService").InputBegan:connect(function(input) 
	
	
	if input.KeyCode == Enum.KeyCode.G and db4 == true then return end 
	if input.KeyCode == Enum.KeyCode.G and db4 == false  
	then 
		print("bar is active")
		db4 = true
		IsShooting = true
		if IsShooting == true then
			script.Parent.Enabled = true
		FillTween:Play()
				FillTween2:Play()
				end
end
game:GetService("UserInputService").InputEnded:connect(function(input)
	
         if isShooting then return end
		IsShooting = true
 
				FillTween:Pause()
				FillTween2:Pause()
		wait(3)
				script.Parent.Enabled = false
				
				FillTween:Cancel()
				FillTween2:Cancel()
--IsShooting = false  // Place it where you need to.
 
			wait(1)
			FillTweenReset:Play()
		FillTween2Reset:Play()
		wait(2)
			db4 = false

end)






1 Like