I'm curious about these line of codes of how it works and what is it called if it has a word

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?

1 Like

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?

2 Likes

So basically a short version of if else statement? Also does “else” replace as “or”?

1 Like

Yeah.

BOOL and VAL1 or VAL2

is equivalent to

if BOOL then
	VAL1
else
	VAL2
end
2 Likes