You can keep the none if you really want to, but highwon example is better because it sets a default but still has a minor flaw. When the mayor is found, it’ll continue through the loop ( max players 128, so it isn’t a major issue, but can be improved ). Add break; right after currentMayor = v:Name( );
break; will cause the loop to end, and putting it in the if will cause it to end as soon as the mayor is found. This turns a guaranteed n run-time expense for best and worst case to n for worst and 1 for best.
It can be improved even further down to O( 1 ) if you can only have 1 mayor maximum.
// Language support
language.Add( "nobody", "Nobody" );
currentMayor = currentMayor || language.GetPhrase( "nobody" );
// Find the current mayor, update the global variable ( although we don't need to do that when you have a function like this which can return the mayor without needing to loop ) with the mayor name
function findCurrentMayor( )
// Reset the global var; not needed with this function...
currentMayor = language.GetPhrase( "nobody" );
// Grab all players on this team; if it is limited to a max of 1 player then we don't need to loop. Even if it isn't limited, we can still grab first in list unless there is support for something else...
local _team_players = team.GetPlayers( TEAM_MAYOR );
// Grab the first element in the list
local _mayor = _team_players[ 1 ];
// If it is valid, then we have a mayor
if ( IsValid( _mayor ) ) then
// Setup the name because we're using it twice
local _name = _mayor:Name( );
// Update the global var; not needed with this function
currentMayor = _name;
// Return the name; this is how it could work, so you can just use findCurrentMayor( ) and it'll return the name without needing to manage a global var