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

How do I check if a statement is true, then continue the scripts?

Asked by
Sorukan 240 Moderation Voter
5 years ago

Is there a way to check something and if it’s false, then completely ignore the line of code and continue with the scripts below it, and if it’s true then run that line of code along with the ones below it.

0
if statements EpicMetatableMoment 1444 — 5y
0
You just defined an if statement perfectly, Sorukan. User#25115 0 — 5y
0
if true then AnotherPerson_999 28 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Question

How do I check if a statement is true, then continue the scripts?

Answer

Yes most certainly Like almost every programming language, lua has a system for conditionals. In lua you can check for a condition and run the code inside the scope of the block.

(hey if this helped you should click "accept answer")

Example Code

if (condition1) then
    print"a"
    --// runs if condition1 is true
elseif (condition2) then
    print"b"
    --// runs if condition2 is true but not if condition1 was true
else
    print"c"
    --// runs if no other conditions worked
end

print"d"
--// runs eitherway since its outside the scope of the if statement

More Specific Example

Is there a way to check something and if it’s false, then completely ignore the line of code and continue with the scripts below it, and if it’s true then run that line of code along with the ones below it.

Yes there certainly is just put your code in a if statement or a function that is called if the statement is true.

local function wasTrue()
    --// some code
end

local function wasFalse()
    --// some other code
end

if (condition) then
    wasTrue()
else
    wasFalse()
end
0
Note that you do not need "()" around conditions in Lua. ex, "if condition1 then" works just fine. Also, a note for anyone new to Lua: saying print"a" is a shortcut for print("a") -- this works for any function where you only want to send a single string as an argument (you can also send new tables this way). chess123mate 5873 — 5y
Ad

Answer this question