diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index ee325baf9..f82b9b646 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -438,8 +438,9 @@ static int smlua__get_field(lua_State* L) { return 0; } - u32 key = lua_tointeger(L, 2); - if (!key) { + int isNum; + u32 key = lua_tointegerx(L, 2, &isNum); + if (!isNum) { const char *key = lua_tostring(L, 2); if (key && key[0] == '_') { if (strcmp(key, "_lot") == 0) { @@ -455,6 +456,11 @@ static int smlua__get_field(lua_State* L) { return 0; } + if (key == 0) { + LOG_LUA_LINE("Key is out of bounds for array: key '%u' (help: array starts at 1)", key); + return 0; + } + key--; // Lua is +1 indexed if (key >= data->count) { LOG_LUA_LINE("Key is out of bounds for array: key '%u'", key);