The topic says the question.
local active = false
while task.wait(0.5) do
local x = active and 5 or -5
print(x)
active = not active
end
Any helpful information or explanation?
The topic says the question.
local active = false
while task.wait(0.5) do
local x = active and 5 or -5
print(x)
active = not active
end
Any helpful information or explanation?
It’s basically just a while true loop, with a 0.5 second delay. It then has the value of x alternate between 5 and -5.
The
local x = active and 5 or -5
thing is called a Ternary operator, or a short hand if else.
Below is the same code, but rewritten such that it may be clearer what it does:
local active = false
while true do
task.wait(0.5) -- Wait 0.5 seconds
local x
if active == true then
x = 5
else
x = -5
end
print(x)
if active == true then
active = false
else
active = true
end
end
Is that any clearer?
So basically a short version of if else statement? Also does “else” replace as “or”?
Yeah.
BOOL and VAL1 or VAL2
is equivalent to
if BOOL then
VAL1
else
VAL2
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.