New feature? for-and loop

Today I accidentally wrote this for my command line.

 s = {}; for i, v in game.CollectionService:GetTagged("Movable") and game.CollectionService:GetTagged("Selectable") do table.insert(s, v) if not v:GetAttribute("Limit") then v:SetAttribute("Limit",100) end end ; game.Selection:Set(s);print(s)

I did not notice that I have written an and in the for loop. Yet it did not error but rather merged the tables. This is definetly NOT a Lua feature but rather a Luau feature. I did not find any documentation or reports on this either.

This is not a feature, but more like a different semantic thing:

local condition = false
local testTable = {1, 2, 3}

for _, number in condition and testTable or {1, 1, 1} do
    print(number) -- 1, 1, 1
end

for _, number in #testTable == 0 and {4, 4, 4} or {1, 2, 3} do
	print(number) -- 1, 2, 3
end

The structure you’re looking at is…
<condition> and <value1> or [<condition> and] <value2>.

Therefore,

-- This
game.CollectionService:GetTagged("Movable") and game.CollectionService:GetTagged("Selectable")

-- is translated into this, as long the condition is neither false or nil
true and game.CollectionService:GetTagged("Selectable")
3 Likes

Ohh, that is my bad. Thanks for the explanations seems that I misunderstood the syntax.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.