Gamemode Error


[ERROR] gamemodes/fragdeathmatch/gamemode/init.lua:43: attempt to index global 'ply' (a nil value)
  1. unknown - gamemodes/fragdeathmatch/gamemode/init.lua:43



The actual code…


// Making this equal

local RDM = math.random( 1, 2 )
	if RDM == 1 then 
	ply:SetTeam( 1 )
	elseif RDM == 2 then
		ply:SetTeam( 2 )
end
RED = team.NumPlayers( 1 )
BLUE = team.NumPlayers( 2 )
	if RED > BLUE then 
		ply:SetTeam( 2 )
	elseif BLUE > RED then
		ply:SetTeam( 1 )
end

Do I have to put ( ply ) after RDM?

Where are you defining ply?

Further up the code.


function GM:PlayerSpawn( ply )
    if ply:Team()== 1 then
	    ply:Give("weapon_crowbar")
		ply:Give("weapon_pistol")
	end
	    if ply:Team()== 2 then
	    ply:Give("weapon_stunstick")
		ply:Give("weapon_357")
	end
	    if ply:Team()== 3 then
	    ply:SetRunSpeed( 2000 )
	end
		//if ply:Team() != 3 then ply:SetTeam(2) end
       // ply:SpectateEntity(Entity(1)) -- Target first player
       // ply:Spectate( OBS_MODE_ROAMING ) 
end
// Making this equal

local RDM( ply ) = math.random( 1, 2 )
	if RDM == 1 then 
	ply:SetTeam( 1 )
	elseif RDM == 2 then
		ply:SetTeam( 2 )
end
RED = team.NumPlayers( 1 )
BLUE = team.NumPlayers( 2 )
	if RED > BLUE then 
		ply:SetTeam( 2 )
	elseif BLUE > RED then
		ply:SetTeam( 1 )
end

Should I remove the end so the code continues through the the equal part?

Edit: I think I see my mistake! I forgot to make the code ignore the end, maybe that may fix it!

You’ve defined it in the scope of PlayerSpawn, more specifically, it’s an argument to PlayerSpawn. You haven’t defined it anywhere…

What you’re probably trying to do is this although I recommend setting up all hooks properly such as PlayerSpawn, PlayerLoadout, PlayerSetModel, etc…

[lua]function GM:PlayerSpawn( ply )
local _team = ply:Team( );

if ( _team == TEAM_SPECTATOR || _team == TEAM_UNASSIGNED ) then
	ply:SetTeam( math.random( 1, 2 ) )
end

local _teamRedCount = team.NumPlayers( 1 )
local _teamBlueCount = team.NumPlayers( 2 )

if ( _teamRedCount > _teamBlueCount ) then 
	ply:SetTeam( 2 );
elseif ( _teamBlueCount > _teamRedCount ) then
	ply:SetTeam( 1 );
end

if ( ply:Team( ) == 1 ) then
	ply:Give("weapon_crowbar")
	ply:Give("weapon_pistol")
elseif ( ply:Team( ) == 2 ) then
	ply:Give("weapon_stunstick")
	ply:Give("weapon_357")
elseif ( ply:Team( ) == 3 ) then // Not sure where 3 comes from...
	ply:SetRunSpeed( 2000 );
end

end[/lua]