How can I use boolean value as a number?

Hi,
I’m writing a function with only one parameter whose type is boolean but I can’t change its transparency because it’s not the same like in other languages (false = 0, true = 1). I want to convert a boolean value into a number value.

1 Like

You can try

local value = if boolValue then 1 else 0

If boolValue is true then set to 1 else set to 0 as you said in the brackets

You can also do it as

local value = 0
if boolValue then
    value = 1
end

But if you can do it as a simple ternary operator it would be easier to understand, but preference

If you don’t know what the ternary operator is, what I did was Lua’s version of it since it doesn’t have a true ternary,

The syntax is like this

local var = condition and (value_if_true) or (value_if_false)

You can also do it directly without needing to use a variable as it returns something

part.Transparency = if boolValue then 1 else 0

@Forummer Thank you for telling me that, haven’t been on Roblox in a long while so it was new information for me, edited my post to include the newer syntax

5 Likes

local Value = if Bool then 1 else 0

Ternary operators are supported by Luau (this pattern is not native to Lua).

4 Likes