Dealing with Panel.Think

I made a post earlier about drawing a scoreboard. I learned that I needed to draw that scoreboard every frame for it to update.

The problem is it just loops into oblivion whenever the panel is active.
http://cloud.steampowered.com/ugc/596931038206034271/A9A1B2BF5E97EE48FCEB004E29E5E6AA07F8DD36/

So how do I stop it from doing that, using code similar to this?



local Plys = vgui.Create( "DPanel" )
			Plys:SetPos( 0, 0 )
			Plys:SetSize( 260, 440 )
		 
		local Playerlist = vgui.Create("DListView")
			Playerlist:SetParent(Plys)
			Playerlist:SetPos(0, 0)
			Playerlist:SetSize(260, 440)
			Playerlist:SetMultiSelect(false)
			Playerlist:AddColumn("Name")
			Playerlist:AddColumn("Amount of kills")
	
		function Playerlist:Think()
			for k,v in pairs(player.GetAll()) do
			Playerlist:AddLine( v:Nick(), v:Frags(), v:Team())
			end
			if #player.GetAll() == 0 then
			Playerlist:AddLine("No Players Online")
			end 
		end	


I’ve been looking around for a while now and I can’t find any documentation that tells me exactly what to do.

Basically I want the scoreboard to update when the panel is active, but only once.

You don’t need to add all players on Think, you just need to on player join and/or when the players score changes.

Panels are persistent, means when you add something the thing you added before will stay there of course.

I’m not exactly sure how to do that. Got any examples?

The best thing to do would just be to refresh the list whenever it is opened.

That’s what I’m trying to do, only I don’t know how.

Can you paste me all of the code please?

All the code is in my first post.

You could do something like this.

[lua]local p = {}
function Playerlist:Think()
for k,v in pairs§ do if not IsValid(v) then p[k] = nil; end end
for k,v in pairs(player.GetAll()) do
if not p[v:SteamID()] then
p[v:SteamID()] = v;
Playerlist:AddLine( v:Nick(), v:Frags(), v:Team());
end
end
if #player.GetAll() == 0 then
Playerlist:AddLine(“No Players Online”)
end
end[/lua]

But again, that’s inefficient. It’s better to update the player list just when the menu is opened.

In the picture you posted you have more than just a panel, paste me it all.



include( "shared.lua" )
include( "hud.lua" )
/* include( "tabmenu.lua" ) */


--[[ Hides the original sandbox HL2 HUD ]]--

	function hidehud(name)
		for k, v in pairs
		{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
			if name == v then return false end
		end
	end
	hook.Add("HUDShouldDraw", "hidehud", hidehud)
	
--[[ Derma For The Team Selection Menu ]]--

	function GM:Initialize() --Responsible for running the entire TAB menu
	
		//Background for everything
		teampanel = vgui.Create( "DFrame" )
		teampanel:SetPos( 10, ScrH() * 0.44 )
		teampanel:SetSize( 300, 500 )
		teampanel:SetTitle( "" )
		teampanel:SetVisible( false )
		teampanel:SetDraggable( false )
		teampanel:ShowCloseButton( false )
		teampanel:MakePopup()
		teampanel.Paint = function()
			draw.RoundedBox ( 10, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) )
		end
		
		//This controls the tabs within the menu
		local TeamSelect = vgui.Create( "DPropertySheet" )
		TeamSelect:SetParent( teampanel )
		TeamSelect:SetPos( 15, 15 )
		TeamSelect:SetSize( 270, 470 )

		//Contains all team switch buttons
		local TeamList = vgui.Create ( "DPanel" )
		TeamList:SetPos( 5, 0 )
		TeamList:SetSize( 240, 440 )
		TeamList.Paint = function()
			draw.RoundedBox ( 0, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) )
		end
		
			local padding = 55
			local CITmale = padding
			local CITfemale = padding
			local Medic = padding
			local Tech = padding
			local Rebel = padding
			
			//Set Team: CitizenMale
			local JoinTeam = vgui.Create( "DButton" )
			JoinTeam:SetParent( TeamList )
			JoinTeam:SetPos( 10, 10 )
			JoinTeam:SetSize( 100, 50 )
			JoinTeam:SetText( "Citizen (Male)" )
			JoinTeam.DoClick = function()
				RunConsoleCommand( "citizenmale" )
			end
			
			//Set Team: CitizenFemale	
			local JoinTeam = vgui.Create( "DButton" )
			JoinTeam:SetParent( TeamList )
			JoinTeam:SetPos( 10, 10 + CITmale )
			JoinTeam:SetSize( 100, 50 )
			JoinTeam:SetText( "Citizen (Female)" )
			JoinTeam.DoClick = function()
				RunConsoleCommand( "citizenfemale" )
			end
			
			//Set Team: Medic
			local JoinTeam = vgui.Create( "DButton" )
			JoinTeam:SetParent( TeamList )
			JoinTeam:SetPos( 10, 10 + CITmale + CITfemale )
			JoinTeam:SetSize( 100, 50 )
			JoinTeam:SetText( "Medic" )
			JoinTeam.DoClick = function()
				RunConsoleCommand( "medic" ) 
			end
			
			//Set Team: Tech
			local JoinTeam = vgui.Create( "DButton" )
			JoinTeam:SetParent( TeamList )
			JoinTeam:SetPos( 10, 10 + CITmale + CITfemale + Medic )
			JoinTeam:SetSize( 100, 50 )
			JoinTeam:SetText( "Tech Specialist" )
			JoinTeam.DoClick = function()
				RunConsoleCommand( "tech" )
			end
			
			//Set Team: Rebel
			local JoinTeam = vgui.Create( "DButton" )
			JoinTeam:SetParent( TeamList )
			JoinTeam:SetPos( 10, 10 + CITmale + CITfemale + Medic + Tech )
			JoinTeam:SetSize( 100, 50 )
			JoinTeam:SetText( "Rebel Soldier" )
			JoinTeam.DoClick = function()
				RunConsoleCommand( "rebel" ) 
			end

		//Temporary Inventory Holder	
		local Inventory = vgui.Create ( "DPanel" )
		Inventory:SetPos( 5, 0 )
		Inventory:SetSize( 240, 440 )
		Inventory.Paint = function()
			draw.RoundedBox ( 0, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) ) 
			end
		
		
		// Derma for player list
		
		
local Plys = vgui.Create( "DPanel" )
			Plys:SetPos( 0, 0 )
			Plys:SetSize( 260, 440 )
		 
		local Playerlist = vgui.Create("DListView")
			Playerlist:SetParent(Plys)
			Playerlist:SetPos(0, 0)
			Playerlist:SetSize(260, 440)
			Playerlist:SetMultiSelect(false)
			Playerlist:AddColumn("Name")
			Playerlist:AddColumn("Amount of kills")
	
		function Playerlist:Think()
			for k,v in pairs(player.GetAll()) do
			Playerlist:AddLine( v:Nick(), v:Frags(), v:Team())
			end
			if #player.GetAll() == 0 then
			Playerlist:AddLine("No Players Online")
			end 
		end
			
		
		TeamSelect:AddSheet( "Class Select", TeamList, "gui/silkicons/user", false, false, "Choose Your Class" )
		
		TeamSelect:AddSheet( "Inventory", Inventory, "gui/silkicons/user", false, false, "Your Current Inventory" )
		
		TeamSelect:AddSheet( "Player List", Plys, "gui/silkicons/user", false, false, "Who's Playing?" )
	
	end


--[[ Functions for toggling visibility of the TAB menu ]]--
	-- These overwrite the scoreboard
	
	function GM:ScoreboardShow()
		teampanel:SetVisible( true )	
	end

	function GM:ScoreboardHide()
		teampanel:SetVisible( false )
	end


[lua]
include( “shared.lua” )
include( “hud.lua” )
/* include( “tabmenu.lua” ) */

–[[ Hides the original sandbox HL2 HUD ]]–

function hidehud(name)
	for k, v in pairs
	{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "hidehud", hidehud)

–[[ Derma For The Team Selection Menu ]]–

function GM:Initialize() --Responsible for running the entire TAB menu

	//Background for everything
	teampanel = vgui.Create( "DFrame" )
	teampanel:SetPos( 10, ScrH() * 0.44 )
	teampanel:SetSize( 300, 500 )
	teampanel:SetTitle( "" )
	teampanel:SetVisible( false )
	teampanel:SetDraggable( false )
	teampanel:ShowCloseButton( false )
	teampanel:MakePopup()
	teampanel.Paint = function()
		draw.RoundedBox ( 10, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) )
	end
	
	//This controls the tabs within the menu
	local TeamSelect = vgui.Create( "DPropertySheet" )
	TeamSelect:SetParent( teampanel )
	TeamSelect:SetPos( 15, 15 )
	TeamSelect:SetSize( 270, 470 )

	//Contains all team switch buttons
	local TeamList = vgui.Create ( "DPanel" )
	TeamList:SetPos( 5, 0 )
	TeamList:SetSize( 240, 440 )
	TeamList.Paint = function()
		draw.RoundedBox ( 0, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) )
	end
	
		local padding = 55
		local CITmale = padding
		local CITfemale = padding
		local Medic = padding
		local Tech = padding
		local Rebel = padding
		
		//Set Team: CitizenMale
		local JoinTeam = vgui.Create( "DButton" )
		JoinTeam:SetParent( TeamList )
		JoinTeam:SetPos( 10, 10 )
		JoinTeam:SetSize( 100, 50 )
		JoinTeam:SetText( "Citizen (Male)" )
		JoinTeam.DoClick = function()
			RunConsoleCommand( "citizenmale" )
		end
		
		//Set Team: CitizenFemale	
		local JoinTeam = vgui.Create( "DButton" )
		JoinTeam:SetParent( TeamList )
		JoinTeam:SetPos( 10, 10 + CITmale )
		JoinTeam:SetSize( 100, 50 )
		JoinTeam:SetText( "Citizen (Female)" )
		JoinTeam.DoClick = function()
			RunConsoleCommand( "citizenfemale" )
		end
		
		//Set Team: Medic
		local JoinTeam = vgui.Create( "DButton" )
		JoinTeam:SetParent( TeamList )
		JoinTeam:SetPos( 10, 10 + CITmale + CITfemale )
		JoinTeam:SetSize( 100, 50 )
		JoinTeam:SetText( "Medic" )
		JoinTeam.DoClick = function()
			RunConsoleCommand( "medic" ) 
		end
		
		//Set Team: Tech
		local JoinTeam = vgui.Create( "DButton" )
		JoinTeam:SetParent( TeamList )
		JoinTeam:SetPos( 10, 10 + CITmale + CITfemale + Medic )
		JoinTeam:SetSize( 100, 50 )
		JoinTeam:SetText( "Tech Specialist" )
		JoinTeam.DoClick = function()
			RunConsoleCommand( "tech" )
		end
		
		//Set Team: Rebel
		local JoinTeam = vgui.Create( "DButton" )
		JoinTeam:SetParent( TeamList )
		JoinTeam:SetPos( 10, 10 + CITmale + CITfemale + Medic + Tech )
		JoinTeam:SetSize( 100, 50 )
		JoinTeam:SetText( "Rebel Soldier" )
		JoinTeam.DoClick = function()
			RunConsoleCommand( "rebel" ) 
		end

	//Temporary Inventory Holder	
	local Inventory = vgui.Create ( "DPanel" )
	Inventory:SetPos( 5, 0 )
	Inventory:SetSize( 240, 440 )
	Inventory.Paint = function()
		draw.RoundedBox ( 0, 0, 0, teampanel:GetWide(), teampanel:GetTall(), Color( 0, 0, 0, 150 ) ) 
		end
	
	
	// Derma for player list

local Plys = vgui.Create( “DPanel” )
Plys:SetPos( 0, 0 )
Plys:SetSize( 260, 440 )

	local Playerlist = vgui.Create("DListView")
		Playerlist:SetParent(Plys)
		Playerlist:SetPos(0, 0)
		Playerlist:SetSize(260, 440)
		Playerlist:SetMultiSelect(false)
		Playerlist:AddColumn("Name")
		Playerlist:AddColumn("Amount of kills")
		teampanel.PlayerList = Playerlist

	local function teampanel:RefreshPlayers()
		self.PlayerList:Clear(true)
		for k,v in pairs(player.GetAll()) do
			self.PlayerList:AddLine( v:Nick(), v:Frags(), v:Team())
		end
		if #player.GetAll() == 0 then
			self.PlayerList:AddLine("No Players Online")
		end 
	end
		
	
	TeamSelect:AddSheet( "Class Select", TeamList, "gui/silkicons/user", false, false, "Choose Your Class" )
	
	TeamSelect:AddSheet( "Inventory", Inventory, "gui/silkicons/user", false, false, "Your Current Inventory" )
	
	TeamSelect:AddSheet( "Player List", Plys, "gui/silkicons/user", false, false, "Who's Playing?" )

end

–[[ Functions for toggling visibility of the TAB menu ]]–
– These overwrite the scoreboard

function GM:ScoreboardShow()
	teampanel:SetVisible( true )	
	teampanel:RefreshPlayers()
end

function GM:ScoreboardHide()
	teampanel:SetVisible( false )
end

[/lua]

Try this.