diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 99c070879af4543842e4723c357d518ed4c5f99f..ac3d081eec2f7d20bf15a6bc2c41638d4f959b12 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -845,7 +845,6 @@ static int lib_pGetMobjGravity(lua_State *L)
 static int lib_pWeaponOrPanel(lua_State *L)
 {
 	mobjtype_t type = luaL_checkinteger(L, 1);
-	//HUDSAFE
 	if (type >= NUMMOBJTYPES)
 		return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1);
 	lua_pushboolean(L, P_WeaponOrPanel(type));
@@ -855,9 +854,8 @@ static int lib_pWeaponOrPanel(lua_State *L)
 static int lib_pGetTeamFlag(lua_State *L)
 {
 	int team = luaL_checkinteger(L, 1);
-	if (team < 0 || team >= numteams)
-		return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1);
-	NOHUD
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	INLEVEL
 	LUA_PushUserdata(L, P_GetTeamFlag(team), META_MOBJ);
 	return 1;
@@ -866,9 +864,8 @@ static int lib_pGetTeamFlag(lua_State *L)
 static int lib_pGetTeamFlagMapthing(lua_State *L)
 {
 	int team = luaL_checkinteger(L, 1);
-	if (team < 0 || team >= numteams)
-		return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1);
-	NOHUD
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	INLEVEL
 	LUA_PushUserdata(L, P_GetTeamFlagMapthing(team), META_MAPTHING);
 	return 1;
@@ -1854,6 +1851,19 @@ static int lib_pPlayerShouldUseSpinHeight(lua_State *L)
 	return 1;
 }
 
+static int lib_pPlayerHasTeamFlag(lua_State *L)
+{
+	player_t *player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
+	INT32 team = (INT32)luaL_checkinteger(L, 2);
+	INLEVEL
+	if (!player)
+		return LUA_ErrInvalid(L, "player_t");
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
+	lua_pushboolean(L, P_PlayerHasTeamFlag(player, team));
+	return 1;
+}
+
 // P_MAP
 ///////////
 
@@ -2490,8 +2500,8 @@ static int lib_pMobjTouchingTeamBase(lua_State *L)
 	INLEVEL
 	if (!mo)
 		return LUA_ErrInvalid(L, "mobj_t");
-	if (team <= 0 || team >= teamsingame)
-		luaL_error(L, "team index %d out of range (1 - %d)", team, teamsingame-1);
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
 	LUA_PushUserdata(L, P_MobjTouchingTeamBase(mo, team), META_SECTOR);
 	return 1;
 }
@@ -2657,8 +2667,8 @@ static int lib_pTeamHasFlagAtBase(lua_State *L)
 {
 	INT32 team = luaL_checkinteger(L, 1);
 	INLEVEL
-	if (team <= 0 || team >= teamsingame)
-		luaL_error(L, "team index %d out of range (1 - %d)", team, teamsingame-1);
+	if (team <= 0 || team >= numteams)
+		luaL_error(L, "team index %d out of range (1 - %d)", team, numteams-1);
 	lua_pushboolean(L, P_TeamHasFlagAtBase(team));
 	return 1;
 }
@@ -3697,8 +3707,8 @@ static int lib_gAddGametype(lua_State *L)
 					if (idx >= 0 && idx < MAXTEAMS)
 					{
 						int team_index = luaL_checkinteger(L, -1);
-						if (team_index < 0 || team_index >= numteams)
-							luaL_error(L, "team index %d out of range (0 - %d)", team_index, numteams-1);
+						if (team_index <= 0 || team_index >= numteams)
+							luaL_error(L, "team index %d out of range (1 - %d)", team_index, numteams-1);
 
 						teamlist[idx] = (UINT8)team_index;
 
@@ -4415,6 +4425,7 @@ static luaL_Reg lib[] = {
 	{"P_DoFollowMobj",lib_pDoFollowMobj},
 	{"P_PlayerCanEnterSpinGaps",lib_pPlayerCanEnterSpinGaps},
 	{"P_PlayerShouldUseSpinHeight",lib_pPlayerShouldUseSpinHeight},
+	{"P_PlayerHasTeamFlag",lib_pPlayerHasTeamFlag},
 
 	// p_map
 	{"P_CheckPosition",lib_pCheckPosition},
diff --git a/src/lua_infolib.c b/src/lua_infolib.c
index 5ae0c2ddb721165e01ab6a1936c7ffdf69775624..0e5b75d141e358ac4c4b5da1b5a9fdbca8c334d7 100644
--- a/src/lua_infolib.c
+++ b/src/lua_infolib.c
@@ -2046,8 +2046,8 @@ static int gametype_set(lua_State *L)
 				if (i >= 0 && i < MAXTEAMS)
 				{
 					int team_index = luaL_checkinteger(L, -1);
-					if (team_index < 0 || team_index >= numteams)
-						luaL_error(L, "team index %d out of range (0 - %d)", team_index, numteams-1);
+					if (team_index <= 0 || team_index >= numteams)
+						luaL_error(L, "team index %d out of range (1 - %d)", team_index, numteams-1);
 
 					gt->teams.list[i] = (UINT8)team_index;
 
@@ -2418,8 +2418,8 @@ static int teamlist_set(lua_State *L)
 	int team = luaL_checkinteger(L, 3);
 	if (i <= 0 || i > teamlist->num)
 		return luaL_error(L, "array index %d out of range (1 - %d)", i, teamlist->num);
-	if (team < 0 || team >= numteams)
-		return luaL_error(L, "team index %d out of range (0 - %d)", team, numteams - 1);
+	if (team <= 0 || team >= numteams)
+		return luaL_error(L, "team index %d out of range (1 - %d)", team, numteams - 1);
 	teamlist->list[i - 1] = (UINT8)team;
 	return 0;
 }
diff --git a/src/p_local.h b/src/p_local.h
index 5ee5dfd5d2ef288b9ecff04672ab439577f8218e..e6c90b0d1ff3b53b98f74298e4a50b386343c5dd 100644
--- a/src/p_local.h
+++ b/src/p_local.h
@@ -146,6 +146,7 @@ void P_ForceLocalAngle(player_t *player, angle_t angle);
 boolean P_PlayerFullbright(player_t *player);
 boolean P_PlayerCanEnterSpinGaps(player_t *player);
 boolean P_PlayerShouldUseSpinHeight(player_t *player);
+boolean P_PlayerHasTeamFlag(player_t *player, UINT8 team);
 UINT16 P_GetPlayerColor(player_t *player);
 
 boolean P_IsObjectInGoop(mobj_t *mo);
diff --git a/src/p_user.c b/src/p_user.c
index 72b098c149cb12a1ffabff995085dc814ed6c5df..9ded986167f927b4609d387c52b9f4ed76154a51 100644
--- a/src/p_user.c
+++ b/src/p_user.c
@@ -13097,6 +13097,14 @@ boolean P_PlayerShouldUseSpinHeight(player_t *player)
 		|| JUMPCURLED(player));
 }
 
+boolean P_PlayerHasTeamFlag(player_t *player, UINT8 team)
+{
+	if (!G_GametypeHasTeams() || team >= numteams)
+		return false;
+
+	return player->gotflag & teams[team].flag;
+}
+
 UINT16 P_GetPlayerColor(player_t *player)
 {
 	if (G_GametypeHasTeams() && player->ctfteam)