Debounce not working

Debounce isnt working on this script.

	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	local debounce = true
	
	 if debounce == true then
			debounce = false
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = 1 
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .9
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .8
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .7 
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .6
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .5 
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .4
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .4 
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .3
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .2
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = .1
		wait(.1)
		plr.PlayerGui.Blackout.Frame.BackgroundTransparency = 0
		wait(.1)
		wait(8)
			debounce = true
		end
	end)
1 Like

First of all, show the whole script so we can understand the context.

Second: You couldve just did a for loop or tweened for that last part.

First of all - That is the whole script.
Second of all - Thanks for your advice.

Then why are you saying if debounce works or not? in this case it is literally irrelevant if thats the whole script.

Debounce is doing literally nothing in that.

It dosent work, thanks why i made this smh

What do you mean it doesnt work, you aren’t even using it the way it is meant to be.

Debounces are a way to activate a cooldown, in that case it is literally irrelevant because the code only runs once.

Then how do i use it the way its meant to be??

For a thing that actually has to have a cooldown? You used debounce in the right way there BUT it is useless in that code because it only runs once.

Debounce is used to prevent the code of happening again in certain ammount of time, can be like this:

local debounce = true
local cooldown = 2 -- Seconds
someEvent:Connect(function(parameters)
       if debounce then
          debounce = false -- Ensures that the code doesn't run again meanwhile it's already running
          wait(cooldown)
          debounce = true -- Allows the event to restart again.
       end
end)

There are really other ways to use this, this is just the most common one

You’re always setting debounce to true right before checking it, so it does nothing. You have to put the initialization of debounce outside your hit event handler.

first of all the code is messy second you are waiting .1 secnds per each transparency value third you can just use a for loop

like something like this

local part = path.to.part

local debounce = true

part.Touched:Connect(function(hit)
    -- get player
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and debounce then -- check if plr exists and debounce is equal to true
         -- loop through
        debounce = false
        for transparency = 1,0,.1 do
           wait(.1)
           -- change it here
           plr.PlayerGui.Blackout.Frame.BackgroundTransparency = transparency
         end
        debounce = true
    end
end)

something like that should work

it works like you should expect it to be like transparency fade away stuff