lloyAlloy on GitHub
Safe access
Docs

Safe access

Safe access that emits fixed Luau

Updated

Nine operators cover the nil checks that Roblox code writes by hand. Each one has a fixed emit. The compiler never looks at a type.

Alloysafe.aly
1local hp = player?.Character?.Humanoid?.Health ?? 100
2local spawn = workspace->Map->Spawns->Spawn1
3local gui = player.PlayerGui=>Hud=>Health
4cache[key] ??= compute(key)
5local root = model.PrimaryPart!
Emitted Luausame line count
1local _1 = (if player == nil then nil else player.Character) local _2 = (if _1 == nil then nil else _1.Humanoid) local _3 = (if _2 == nil then nil else _2.Health) local hp = (if _3 == nil then 100 else _3)
2_1 = (if workspace == nil then nil else workspace:FindFirstChild("Map")) _2 = (if _1 == nil then nil else _1:FindFirstChild("Spawns")) local spawn = (if _2 == nil then nil else _2:FindFirstChild("Spawn1"))
3local gui = player.PlayerGui:WaitForChild("Hud"):WaitForChild("Health")
4if cache[key] == nil then cache[key] = compute(key) end
5local root = (if model.PrimaryPart == nil then error("model.PrimaryPart is nil") else model.PrimaryPart) return nil
  • ?. ?: ?[ ?( guard one link; one ? guards every later link in the chain.
  • ?? tests nil only, so false survives. ??= assigns once, and an index on the left runs once.
  • -> is FindFirstChild and => is WaitForChild. A name with spaces is a string, a dynamic one goes in brackets.
  • ! asserts not nil and quotes the source text in its message. It is only ever that, so a!(b) needs no parentheses.