You should be really careful with those direct indexing of getComponent.
I did this in my mod, too (very often) and got randomly "Error occured".
I got this info from UG:
There seems to be problems with temporaries, i.e. somehow "fatInstances" contains garbage, even if it is in the local scope. I still have to pin-point the problem exactly.
It seems that the library we are using to interface with C++ (sol2) is not liking the following:
local comp = api.engine.getComponent(id, api.type.ComponentType.MODEL_INSTANCE_LIST)
local fatInstances = comp.fatInstances
the problem should be fixed.
I think this is a general issue with the library, where the lifetime of certain objects is incorrect, so (as a workaround) I suggest you try (as much as possible) to assign the result of "api" calls to temporary variables.
(This is mostly because the result of "api" calls are not lua tables, but is just lua userdata to real game objects or wrappers around them (in order to avoid expensive copies).)
https://github.com/IncredibleHannes/TPF2-Timetables/blob/a803a2a99dddfb49dc9ec0fcad910df9530f259c/res/scripts/celmi/timetables/timetable_helper.lua#L158
You should be really careful with those direct indexing of getComponent. I did this in my mod, too (very often) and got randomly "Error occured".
I got this info from UG: