My for i loop is broken?

hi, the title says it, i think my for loop is broken when i think it shouldent take a look:

local status = game.ReplicatedStorage.Status
local cooldown = 5 -- 240
local currentDay = game.ReplicatedStorage.Day
local mSpeed = game.ReplicatedStorage.MonsterSpeed
local isSleeping = game.ReplicatedStorage.isSleeping

local function timer()
	for i = cooldown, 1, -1 do
		print(cooldown)
		print(status.Value)
		if cooldown == 0 then
			status.Value = "Night"
		end
		task.wait(1)
	end
end
		

spawn(timer)

and yes i have read the documentation and i still think this should work
if you know why its broken pls let me know.

5 Likes

it can’t go to zero because the second value is the for loop in the end value, cooldown can never get to 0 because the value is set to one*

2 Likes

I dont really understand what u mean with “cooldown can never get to 1 because the value is set to one” as to earlier in the script it is set to 5 but it also can be my braindead brain

1 Like

Your code doesn‘t really make sense. Your cooldown variable is 5 and your for loop has an increment of -1. Your end value is 1 but in the for loop it checks if cooldown is 0 which will not be reached as the for loop already stopped.

This should work:

local status = game.ReplicatedStorage.Status
local cooldown = 5 -- 240
local currentDay = game.ReplicatedStorage.Day
local mSpeed = game.ReplicatedStorage.MonsterSpeed
local isSleeping = game.ReplicatedStorage.isSleeping

local function timer()
	for i = cooldown, 0, -1 do
		print(cooldown)
		print(status.Value)
		if cooldown == 0 then
			status.Value = "Night"
		end
		task.wait(1)
	end
end
		

spawn(timer)
2 Likes


dont worry, i didnt explain that well

2 Likes

You are counting down until you are at number 1, the second number is the End Value, meaning it will stop the loop when it reaches 1. So countdown will never reach 0, the second value should be 0

1 Like

Doesnt it mean im setting it to 5, and i want it to go to 0 and going -1 every sec?

1 Like

this would work because the second value is set to 0 (end value)

1 Like

I see what yall mean, but it does nothing, it doesnt count down my output looks like this:

https://gyazo.com/82fa109957f6d2000776151c04099e89

why isnt it counting down?

1 Like

The last value should be set to -1

But it is set to -1 so it should go down but it isnt going down

1 Like

It might not be counting down because you’re printing the wrong variable, try print(i) instead of print(cooldown)

1 Like

Its fixed i just used a while loop and it works fine

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.