Why isn't the debounce working

  1. What do you want to achieve?
    A selling system that works!

  2. What is the issue?
    The issue is when you go to sell the debounce doesn’t work and spams the sound I added in!

  3. What solutions have you tried so far?
    Finding a solution

script.Parent.Touched:Connect(function(h)
	if h.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(h.Parent)
		local sound = script.Parent.CoinSound

		local debounce = false

		if debounce == false then
			debounce = true
			
			if player then
				local leaderstats = player:WaitForChild("leaderstats")
				local currency = leaderstats.Coins
				local selling = leaderstats.Strength

				if selling.Value > 0 then
					sound:Play()
					wait(0.3)
					currency.Value = currency.Value + selling.Value
					selling.Value = 0
					player.Level.Exp.Value = player.Level.Exp.Value + 1
					debounce = true
				end
			end
		end
	end
end)

Is it supposed to set debounce to true in the

if selling.Value > 0 then end

?

Basically I want it so if you step on a part it sells currency and then you have to step off the part and step back on it to sell again instead of staying on the part and keep selling currency. If you do not know what I mean its basically a selling place for a simulator.

The debounce isn’t being set back false once completed.

So how would I fix that? (3 0 c h a r)

So it’s like those money collectors in tycoon games?

No, its like a selling place in a simulator.

What happens if you set debounce to false* instead of true* on the last few lines?

It is still doing the same thing

it should look something more like this:

script.Parent.Touched:Connect(function(h)
	if h.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(h.Parent)
		local sound = script.Parent.CoinSound

		local debounce = false

		if debounce == false then
	
			if player then
				local leaderstats = player:WaitForChild("leaderstats")
				local currency = leaderstats.Coins
				local selling = leaderstats.Strength

				if selling.Value > 0 then
                    debounce = true
					sound:Play()
					wait(0.3)
					currency.Value = currency.Value + selling.Value
					selling.Value = 0
					player.Level.Exp.Value = player.Level.Exp.Value + 1
					debounce = false
				end
			end
		end
	end
end)

It still does the same thing I keep walking on the part and it keeps making the sound play

put the variable outside the function, not inside

1 Like

sorry, a little brain fart, use this:

local debounce = false

script.Parent.Touched:Connect(function(h)
	if h.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(h.Parent)
		local sound = script.Parent.CoinSound

		if debounce == false then
	
			if player then
				local leaderstats = player:WaitForChild("leaderstats")
				local currency = leaderstats.Coins
				local selling = leaderstats.Strength

				if selling.Value > 0 then
                    debounce = true
					sound:Play()
					wait(0.3)
					currency.Value = currency.Value + selling.Value
					selling.Value = 0
					player.Level.Exp.Value = player.Level.Exp.Value + 1
					debounce = false
				end
			end
		end
	end
end)

Thank you, it has worked! Have a good day!

local debounce = false

script.Parent.Touched:Connect(function(h)
	if h.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(h.Parent)
		local sound = script.Parent.CoinSound

		if debounce then
			debounce = false
			task.wait(5)
			return
		end
		
		if player then
			local leaderstats = player:WaitForChild("leaderstats")
			local currency = leaderstats.Coins
			local selling = leaderstats.Strength

			if selling.Value > 0 then
				debounce = true
				sound:Play()
				task.wait(0.3)
				currency.Value = currency.Value + selling.Value
				selling.Value = 0
				player.Level.Exp.Value = player.Level.Exp.Value + 1
			end
		end
	end
end)

Change task.wait(5) to whatever preferred length of time.