Why isn't my loop not working?

For some reason why loop isn’t working even though it should. When a player activates I have a print and it prints correctly but the loop doesn’t work. I have no further explanation sorry.
Script:

local Debounce = 0

while Debounce ~= 0 do

wait(1)

Debounce -= 1

print("Subtracting")

end

--other stuff
Debounce = 10

Well the obvious answer is that it’s not running due to debounce being equal to 0. You made a loop that checks if it is NOT equal to zero (== is equal, ~= is not equal).

Setting the value later won’t matter as the script has already passed that thread. You’re probably thinking it may act like events such as .touched and such, since those activate regardless of position in the script.

The loop would only work once cause it’ll only detect when Debounce ~= 0 and that’s it, it won’t account for the other time when you set it back to 10

You could use a custom function to make this work, perhaps?

local Debounce = 0

local function DebounceCount()
    while Debounce ~= 0 do
        wait(1)
        Debounce -= 1
        print("Subtracting")
    end
end

--other stuff

Debounce = 10
DebounceCount()
1 Like

I want it to be not equal to 0 though. SO when it’s for example like 10 it will keep subtracting till it’s 0.

Then create a custom function as @JackscarIitt has put, then put it straight after the variable has changed.

1 Like

Wrap it in a coroutine so the rest of runs.

local Debounce = 0

coroutine.wrap(function()
while Debounce ~= 0 do
wait(1)
Debounce -= 1
print("Subtracting")
end
end)()

--other stuff
Debounce = 10
1 Like
debounce = 10

for i = debounce, 0, -1 do
   print(i)
end