I want to use an "or" so the script decreases the minutes by 1 and sets the seconds back to 59 and when its not a minute just decrease the seconds by 1 but every time I run the script it just decreases the minutes by 1 and sets the seconds back to 59
local seconds = 0 local minutes = 10 currentTime.Value = "Time - "..minutes..":0"..seconds for i = 600, -1 do wait(1) if i == 600 or 540 or 480 or 420 or 360 or 300 or 240 or 180 or 120 or 60 then minutes = minutes - 1 seconds = 59 elseif i == 0 then seconds = 0 else seconds = seconds - 1 end currentTime.Value = "Time - "..minutes..":"..seconds end end
Firstly, I'd like to point out that you are using or
incorrectly. When you do x == y or z
, y
is compared to x
, but z
is not compared to x
, it is instead treated as its own condition.
The reason for your if
's condition always being met is because in Lua, any value that is not false
or nil
is treated as truthy
in a boolean context, so these examples would print:
if 4 then print(4) end if "text" then print("text") end if {} then print({}) end
Since the conditions used are not false or nil (falsey), the condition is met.
What you are looking for is x == y or x == z
.
However you are checking tons of numbers, and doing if a == b or a == c or a == d or .... then.
would get tedious very quickly. I noticed that the numbers are all divisible by 60, so you can use the modulo operator %
. It basically divides two numbers and returns the remainder after division.
local seconds = 0 local minutes = 10 currentTime.Value = "Time - "..minutes..":0"..seconds for i = 600, -1 do wait(1) if i % 60 == 0 then -- If the result is 0, that is because i is divisible by 60 minutes = minutes - 1 seconds = 59 elseif i == 0 then seconds = 0 else seconds = seconds - 1 end currentTime.Value = "Time - "..minutes..":"..seconds end end
From what I understood from your script you attempted to format a number as timer (e.g. 3:00
). If so, you can use this handy function:
local function timer_format(x) return math.floor(x/60) .. ":" .. (x % 60 < 10 and "0" or "") .. x % 60 --divides x by 60 and rounded down. If x % 60 < 10 I add "0" to it so --it doesn't look like "3:5", it will look like "3:05". end print(timer_format(120)) -- 2:00