When trying to build a subway element going up, the game crashes with this error:
The mod Subsurface - build beneath your base! (1.1.28) caused a non-recoverable error.
Please report this error to the mod author.
Error while running event Subsurface::on_built_entity (ID 6)
__Subsurface__/scripts/trains.lua:10: attempt to index local 'rail' (a nil value)
stack traceback:
__Subsurface__/scripts/trains.lua:10: in function 'subway_built'
__Subsurface__/control.lua:630: in function <__Subsurface__/control.lua:560>
The cause seems to be that i copy-pasted the segment (from a different level). This makes the rails leftover "subway rails" rather than normal rails. Placing the subway on top of these seems to be what causes the crash - if I remove the 5 tracks underneath where I'm placing, it works.
AI diagnosis and fix (connected with Factorio MCP so grounded in my actual game):
The existing subway-rail entities face north. subway_built() first tries to create new rails, then falls back to finding existing ones with exactly the tunnel’s direction. For a south-facing tunnel, creation fails because the rails already exist, and the fallback misses them because their direction is north. Line 10 then dereferences nil.
Fix: accept existing rails with the same alignment in either direction (north/south or east/west). Also handle failure explicitly: if no compatible rail can be created or found, cancel placement cleanly instead of accessing rail.