Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Debounce not working?

This is very odd, it should work, right? But it's not. You can click it as fast as you please and it's a problem.

Code:

local b = script.Parent
local screen = b.Parent.Screen
local hats = {["Euro 180"] = 401464679, ["Gold Ollie"] = 401464681, ["Red Grind"] = 401464684, ["Purple Indy"] = 401464683, ["Star Tailslide"] = 401464687}
local pose = {"Euro 180", "Gold Ollie", "Red Grind", "Purple Indy", "Star Tailslide"}
local db = false
local price = 100000
local items = game.ServerStorage.hatstorage

b.ClickDetector.MouseClick:connect(function(plr)
	if db == false then
		db = true
		if plr.leaderstats.Coins.Value >= price then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - price
	for key, v in pairs(hats) do
	for i = .1, 4 do
		wait(.1)
		num = hats[pose[math.random(1, #pose)]]
		screen.item.Texture = "rbxassetid://"..num
		end
	end
for key, v in pairs(hats) do
if v == num then
	plr.PlayerGui.congrat.Frame.ya.Text = "You won a "..key.."!"
	wait(2)
	plr.PlayerGui.congrat.Frame.ya.Text = ""
	local won = items:FindFirstChild(key):Clone()
	won.Parent = plr.Inventory
end
end
		end
	end
	wait(.2)
	db = false
end)

1 comment

0

Please tab it correctly..

2 answers

✓ Accepted answer

Your debounce logic is a little flawed. If you look at #33, you can see that you set the debounce value to false outside of the if statement. What this means is actually that even if the debounce is true, it will be set to false 0.2 seconds after it was clicked.

The solution here is to return the function to avoid these little things catching you out. You could simply stick the db=false inside of the condition but what's the joy of that?

local b = script.Parent
local screen = b.Parent.Screen
local hats = {["Euro 180"] = 401464679, ["Gold Ollie"] = 401464681, ["Red Grind"] = 401464684, ["Purple Indy"] = 401464683, ["Star Tailslide"] = 401464687}
local pose = {"Euro 180", "Gold Ollie", "Red Grind", "Purple Indy", "Star Tailslide"}
local db = false
local price = 100000
local items = game.ServerStorage.hatstorage

b.ClickDetector.MouseClick:connect(function(plr)
    if db then return end; -- Stop the function from running. 
	-- Mind that stopping the function is no good if you wanted an else statement later.
        db = true
        if plr.leaderstats.Coins.Value >= price then
            plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - price
    for key, v in pairs(hats) do
    for i = .1, 4 do
        wait(.1)
        num = hats[pose[math.random(1, #pose)]]
        screen.item.Texture = "rbxassetid://"..num
        end
    end
for key, v in pairs(hats) do
if v == num then
    plr.PlayerGui.congrat.Frame.ya.Text = "You won a "..key.."!"
    wait(2)
    plr.PlayerGui.congrat.Frame.ya.Text = ""
    local won = items:FindFirstChild(key):Clone()
    won.Parent = plr.Inventory
end
end
        end
    wait(.2)
    db = false
end)

1 comment

0

this worked, thanks

For future reference: please, please, please indent your code. It makes it easier to read for everyone, and if it was properly indented you may have been able to see the problem already.

After properly indenting the code, the function looks like this:

b.ClickDetector.MouseClick:connect(function(plr)
	if db == false then
		-- Do stuff
	end
	wait(.2)
	db = false
end)

The debounce is set to false whenever the ClickDetector is clicked, whereas it should only be set to false after the code has run. This can be fixed like so:

local b = script.Parent
local screen = b.Parent.Screen
local hats = {["Euro 180"] = 401464679, ["Gold Ollie"] = 401464681, ["Red Grind"] = 401464684, ["Purple Indy"] = 401464683, ["Star Tailslide"] = 401464687}
local pose = {"Euro 180", "Gold Ollie", "Red Grind", "Purple Indy", "Star Tailslide"}
local db = false
local price = 100000
local items = game.ServerStorage.hatstorage

b.ClickDetector.MouseClick:connect(function(plr)
    if db == false then
        db = true
        if plr.leaderstats.Coins.Value >= price then
            plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - price
   			for key, v in pairs(hats) do
			    for i = .1, 4 do
			        wait(.1)
			        num = hats[pose[math.random(1, #pose)]]
			        screen.item.Texture = "rbxassetid://"..num
				end
			end
			for key, v in pairs(hats) do
				if v == num then
				    plr.PlayerGui.congrat.Frame.ya.Text = "You won a "..key.."!"
				    wait(2)
				    plr.PlayerGui.congrat.Frame.ya.Text = ""
				    local won = items:FindFirstChild(key):Clone()
				    won.Parent = plr.Inventory
				end
			end
		end
	    wait(.2)
	    db = false -- The debounce is now set INSIDE of the if statement!
	end
end)

Hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.