[Beta] if local Statements and Expressions


[Update] November 11, 2026


Hi Creators,

We’re launching a new Luau feature, if local and if const, as a Studio beta! This is new syntax that allows you to write:

if local character = player.Character then
  if local humanoid = character:FindFirstChildWhichIsA("Humanoid") then
    if local rootPart = humanoid.RootPart then
       ...
    end
  end
end

Instead of:

local character = player.Character
if character then
  local humanoid = character:FindFirstChildWhichIsA("Humanoid")
  if humanoid then
    local rootPart = humanoid.RootPart
    if rootPart then
       ...
    end
  end
end

You can learn more about this feature and the design behind it by reading the RFC.

Additional Details

Here are some more detailed examples

if const statements
if const player = Players:GetPlayerFromCharacter(character) then
    print(player.Name)
end
if expressions
local name = if local player = Players:GetPlayerFromCharacter(character) then player.Name else "Unknown"
-- equivalently, with const:
local name = if const player = Players:GetPlayerFromCharacter(character) then player.Name else "Unknown"
type narrowing
local function findPlayer(): Player?
    -- player: Player
    print(player.Name)
end

if local player = findPlayer() then
    -- player: Player
end

What’s not supported yet

For this Beta, if local supports a single name and a single value. Multi-return bindings like this are not allowed:

if local ok, result = pcall(riskyFunction) then
    ...
end

Neither are compound conditions like:

if local x = foo(); local y = bar() then
    ...
end

These may be supported in the future.

Try it out

You can enable this now under “If Local/Const Bindings” in Studio Beta Features. For more details, see the RFC.

Give it a try and let us know what you think!
Thank you!

306 Likes

This topic was automatically opened after 11 minutes.

Is this slightly more performant because the variable is only created if the variable is not false or nil?

16 Likes

not really a great addition, removes a line of code, possibly even a step, by returning condition to if and also caching to variable, but not tuple formatted variables? honestly really pointless either way, and can make reading code give you a brain fart, when seeing that

5/10 for not really good or bad, wont use

Unsolicited Constructive Feedback on Adjacent Topic

please add tuples to the accepted value type

Intro Review
local A 
-- in this case A = nil

local A,B = 1

-- you cannot do this because it REQUIRES a value per variable

local A,B = 1,nil

-- I dont get why you have to do this manually but its fine not really the focus

local A,B

-- this will be A = nil and so is B

ok now moving on now to conditions on what kind of value you get


local isInside = (EdgeCount % 2 == 1)

local A = isInside and 1 or 2

-- if  condition was true you get 1
-- otherwise 2

local B = isInside and 1

-- this returns false when isInside = false or nil
Visual Aids Of Where Could've used

Now this is the part that im asking for

-- you cannot do:
local condition = true
local A,B = condition and 1,2 or 2,1

-- expected when true A = 1, B = 2 otherwise its flipped order
-- if you wanted to do this you have to:

-- split:
local A = condition and 1 or 2
local B = condition and 2 or 1

-- table then unpack to tuple:
local A,B = table.unpack(condition and {1,2} or {2,1})

-- flatten:
local A,B
if condition then
 A,B = 1,2
else 
 A,B = 2,1
end

What I am asking for:

local condition = true
local A,B = condition and 1,2 or 2,1


-- also bonus:

local A,B = condition and 1 or 2,1

-- if true then A = 1 /  B = nil else A = 2 / B = 1

Thank you for reading my TED Talk

23 Likes

I couldn’t really see the use-case for it when it was “leaked” via flags, but seeing the RFC I can see the appeal, although it still looks really cursed :sob:

What I am curious on if this runs faster than doing it the traditional way?

6 Likes

If you read the RFC you could probably tell better why this is useful… Instead of complaining that we got a cool new feature you should maybe consider creating an RFC about your great idea.

20 Likes

Does this feature also include the syntax of

while local player = findPlayer() do
   ...
end

Or is it currently limited to conditional statements only? Are there any plans of extending this support to loops as well? It would be really useful for pieces of code like this

local i = 0
while true do
    local target = world:target(e, C, i)
    if not target then
        break
    end
    -- use target
    i += 1
end

Which would just simplify to

local i = 0
while local target = world:target(e, C, i) do
    -- use target
    i += 1
end

Great feature nonetheless.

11 Likes

This is so cursed, I love it.

Does this properly scope said variables within the conditional block?

If so, hot-diggity, I love this kinda rogue performance gain.

2 Likes

This is great, sure it’s not the prettiest, but I do find myself defining variables solely for the condition and variable within an if statement quite often.

9 Likes

This seems like a decent addition, I am curious about any sort of performance impact this might have though.

Omg, how on earth am I going to find all of the examples of this in my codebase to clean them up and switch to this. Hate those variable checking chains.

This only supports nil / false checking, right?

1 Like

great now add switch statements to luau so I can have an even more cursed version of this

local name = switch character
    case const player = Players:GetPlayerFromCharacter(character)
        => player.Name
    case const model = character:FindFirstAncestorOfClass("Model")
        => model.Name
    else
        => "Unknown"
18 Likes

Time to rewrite my framework I guess

1 Like

This did seem redundant until I gave the RFC a lookie look. Indeed seems nice for situations as it states when you want to chain multiple condition checks on a tree of objects. Would be nice if the post used an example more like this.

At the end of the day, purely boils down to preference and I think it could help make code a bit cleaner situationally :slight_smile:

20 Likes

@Yarik_superpro rate this update and tell me if its good please teach me Master

8 Likes

Does this feature also include the syntax of

while local player = findPlayer() do
   ...
end

while local support is in progress! We’re thinking of adding it under this beta when it’s completed :smile:

21 Likes

I love this SO SO much. There are a lot of places in my code that I do genuinely just want to pass a check and continue with that same variable. I always found myself hating the fact that I had to create a variable that I later won’t use, so this is basically a dream come true, no matter how funny it sounds lol.
Even if it’s nothing huge, any addition is more ways to do stuff so thanks!!

5 Likes

Would be nice if the post used an example more like this.

ah thanks for checking out the example in the RFC! We’ll update this post with a similar example

5 Likes

Nice convenience addition, sanity checks at least now can be streamlined

3 Likes

big fan of this, nice qol feature. tnx

2 Likes