diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index e5d477549c0f22879e172912ae56613c36603861..20eaa20489e538a531b688970a63bcd5811e83b7 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -2660,6 +2660,10 @@ static int lib_gAddGametype(lua_State *L)
 	if (!lua_lumploading)
 		return luaL_error(L, "This function cannot be called from within a hook or coroutine!");
 
+	// Ran out of gametype slots
+	if (gametypecount == NUMGAMETYPEFREESLOTS)
+		return luaL_error(L, "Ran out of free gametype slots!");
+
 #define FIELDERROR(f, e) luaL_error(L, "bad value for " LUA_QL(f) " in table passed to " LUA_QL("G_AddGametype") " (%s)", e);
 #define TYPEERROR(f, t) FIELDERROR(f, va("%s expected, got %s", lua_typename(L, t), luaL_typename(L, -1)))
 
@@ -2734,10 +2738,6 @@ static int lib_gAddGametype(lua_State *L)
 	// pop gametype table
 	lua_pop(L, 1);
 
-	// Ran out of gametype slots
-	if (gametypecount == NUMGAMETYPEFREESLOTS)
-		return luaL_error(L, "Ran out of free gametype slots!");
-
 	// Set defaults
 	if (gtname == NULL)
 		gtname = Z_StrDup("Unnamed gametype");