Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script keeps triggering "if" condition?

Asked by
Zrxiis 26
6 years ago

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
0
If you have more than four conditions in an if statement, you should probably revise your script logic. User#25115 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Hopefully this answered your question and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions then feel free to leave them down in the comments below.

Have a wonderful day

0
Almost forgot: "for i = 600, -1" is incorrect, you need to do "for i = 600, -1, -1". You need the increment (since you are decrementing the increment must be negative). User#24403 69 — 6y
0
You know that he could have just put every desired number in a table and check to see if i was one of those numbers, right? DeceptiveCaster 3761 — 6y
0
Yes, but modulo is clearly the better option in this scenario. Especially because the OP is dealing with time and there are 60 seconds in a minute and 60 minutes in an hour. User#25115 0 — 6y
0
This works but it's also possible to just use 2 for loops to get the job done as well Vulkarin 581 — 6y
View all comments (2 more)
0
I'm confused by the modulo operator. What does it do? Zrxiis 26 — 6y
0
When you say "he could have just ..." you make it seem like what you suggest would be way easier. Well no. @MCAndRobloxUnited User#19524 175 — 6y
Ad

Answer this question