Next player in Spectate mode and display spectating players name

  1. So I have been working on a gamemode that needs a spectator mode. I have it all down. When you type “spectate” in console, you join the team spectator and you become invisible and spectate a player. When you type “respawn” in console, you respawn as a normal player with all the normal things a player has. My problem here is that I can’t get it so that when you click your Right Mouse button, it takes you to the next player in spectate mode. Here is the spectate code I have so far:


function spectate()
	local ply = LocalPlayer()
	ply:Spectate(4)
	ply:SetTeam(2)
	ply:SetModel("")
	ply:StripWeapon("weapon_fists")
	
	if (ply:KeyDown(IN_ATTACK2)) then
		ply:UnSpectate()
		ply:Spectate(4)
	end
end
concommand.Add( "spectate", spectate )

function respawn(ply)
	ply:UnSpectate()
	ply:SetTeam(1)
	ply:Give("weapon_fists")
	ply:SetModel("models/player/kleiner.mdl")
end
concommand.Add( "respawn", respawn )


  1. I also am looking to see how to get the players name to display when you are spectating them. I tried doing:


if ply:Spectate() then
    draw.DrawText( ply:Nick(), "Trebuchet24", ScrW()*.50, ScrH()*.81, Color( 0, 255, 0, 255), TEXT_ALIGN_CENTER )
end


But it’s only displaying your nickname and not the person who you are spectating.

Any help is appreciated :slight_smile:

  1. That’s because the spectate function isn’t in a hook that allows for it to work every time a key is pressed. Use this: http://wiki.garrysmod.com/page/GM/KeyPress

  2. What realm is that being run in? Where are you getting the ply variable from?

should have said this:

function SpecName(ply)
if ply:Spectate() then
draw.DrawText( ply:Nick(), “Trebuchet24”, ScrW().50, ScrH().81, Color( 0, 255, 0, 255), TEXT_ALIGN_CENTER )
end
hook.Add(“SpecName”,“SpecName”,SpecName)

[editline]8th March 2015[/editline]

Also. I tried the GM:KeyPress and still isn’t going to the next player when in spectate mode. Any other ideas?

try a for loop with ipairs (player.GetAll table)

-snip-