Why isn't my if statement not working?

I’m trying to make an if statement when true will do a loop 10 times. I tried prints, but it wouldn’t work.

Script:

if Debounce == 10 then
	print("4")--It doesn't print this so I don't think it's doing the if
	for i = 5, 15 do
		wait(1)
		Debounce -= 1
		print("1")
	end
end

Thank you for reading.

1 Like

It’s because for whatever reason, Debounce isn’t 10.

3 Likes

I have a print and when it turns 10 it will print and it prints.

Can you send a larger snippet of the script I am having a hard time understanding what you were trying to achieve.

1 Like

(Also if I forget an end I deleted some of the non important code).
Script:

local Debounce = 0

if Debounce == 10 then
	print("4")
	for i = 5, 15 do
		wait(1)
		Debounce -= 1
		print("1")
	end
end

UserInputService.InputBegan:Connect(function(Input, Chatted)
	if Chatted then
		return
	end

if Input.KeyCode == Enum.KeyCode.Q then
		if Debounce == 0 then
			print("2")
			Event:FireServer(mouse.hit.p)
			Debounce = true
			wait(0.1)
			Debounce = 10
			print("3")
		end
	end
end)
1 Like

Are you ever defining debounce?

This is very important code there’s a lot happening some of which is not correctly made. I don’t know where to begin. I’m trying to find the big picture like what it’s supposed to do. Very confused. I see debounce is set to true for 0.1 Seconds then to 10 again

1 Like

I forgot to add that sorry. I edited the code.

This is a cooldown for a ability. So when activated I need to be 10 again.

Okay here is what you should do if u want that to keep running

place that at the very bottom

while true do
wait()
if Debounce == 10 then
	print("4")
	for i = 5, 15 do
		wait(1)
		print(Debounce)
		Debounce -= 1
	end
end
end

When you placed it at the top outside of a loop it only ran once(And debounce was 0 so ofc it wouldnt run) but if you want it to keep running u need to place it below all the code/functions so they may run

1 Like

You said

This is a lot of code for a cooldown

With the addition of the while true loop, you need to place at the bottom… overall it would be a lot of code just for a cooldown. Not to mention it isn’t recommend to have loops placed into loops I believe that’s considered bad practice

Just an example of what I would of done is keep debounce as how it usually is a bool value

if Debounce == false then
Debounce = true
 print("Ability Activated")
 Event:FireServer(mouse.hit.p)
wait(10) --//Cooldown
Debounce = false
end

This example wouldn’t need loops within loops to work just a wait(Cooldown)

You have to use a UIS for “Input.Keycode” this requires you to use a remoteEvent.

Oh sorry, i didn’t see that line but I still think he needs a UIS

There are several major issues that are causing issues. The first is that you declare the Debounce variable as 0 right before the if statement is ran. That if statement isn’t in a function or connected to an event, so it will fire right after Debounce is set to 0, but never again. You should set it as a function and then run it after you set Debounce to 10 later in the code.

As well as this, setting Debounce to True may also cause issues due to True being a boolean value and 10 being a number value, both not being compatible with each other. This becomes an issue if Debounce is set to True while another part of the script is trying to run Debounce -= 1.

local UserInputService = game:GetService("UserInputService")
local Debounce = 0

function checkDebounce()
if Debounce == 10 then
    print("4")
        for i = 5, 15 do
	        wait(1)
	        Debounce -= 1
            print("1")
        end
    end
end

UserInputService.InputBegan:Connect(function(Input, Chatted)
    if Chatted then
	    return
    end

    if Input.KeyCode == Enum.KeyCode.Q then
		if Debounce == 0 then
		    print("2")
			Event:FireServer(mouse.hit.p)
			wait(0.1)
			Debounce = 10
            checkDebounce()
			print("3")
		end
	end
end)

Now this does still have some minor issues, but this should solve anything major. It didn’t appear that the Debounce = True was ever needed anywhere in the script, so it would be best to outright remove it in this case as well.

Hope this helps.

Well the problem I have is it’s i =5,15
Why are you using 5 and 15?
I would use 1 and 10 that makes it a lot less confusing.

If you wrote function like this:

function foo()
    for i=5,15 do
        print(i - 4)
    end
end

the output would be:

1
2
3
4
5
6
7
8
9
10
11

Meaning, you’re code would make debounce == -1 and since -1 ~= 0, then it could only be used once, and never again if the player waits 11 seconds instead of 10.

I suggest the following code

if Input.KeyCode == Enum.KeyCode.Q then
    if Debounce == true then
        Event:FireServer(mouse.hit.p)
        wait()
        Debounce = false
        wait(DELAY)  -- DELAY = 10 (or whatever you would like it to be)
        Debounce = true
    end
end

I hope that helps!

1 Like