diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 519801f2f6d174bdad220c99afd5fd01af0287db..d56f49a5b0b496bab94450f468fc90b03e6f5bf7 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -2184,16 +2184,18 @@ static int lib_evStartCrumble(lua_State *L)
 
 static int lib_pGetZAt(lua_State *L)
 {
-	pslope_t *slope = *((pslope_t **)luaL_checkudata(L, 1, META_SLOPE));
 	fixed_t x = luaL_checkfixed(L, 2);
 	fixed_t y = luaL_checkfixed(L, 3);
 	//HUDSAFE
-	if (slope)
-		lua_pushfixed(L, P_GetSlopeZAt(slope, x, y));
-	else
+	if (lua_isnil(L, 1))
 	{
 		fixed_t z = luaL_checkfixed(L, 4);
-		lua_pushfixed(L, P_GetZAt(slope, x, y, z));
+		lua_pushfixed(L, P_GetZAt(NULL, x, y, z));
+	}
+	else
+	{
+		pslope_t *slope = *((pslope_t **)luaL_checkudata(L, 1, META_SLOPE));
+		lua_pushfixed(L, P_GetSlopeZAt(slope, x, y));
 	}
 
 	return 1;