Derma menu sometimes not opening

I created my first bigger Derma Menu, but I have a little issue.
The derma menu is not opening everytime I want it to be opened. The issue seems to be really random.
Here’s the code to open it:
Serverside:



--Check when the notifier should be opened and send a net message then 
--end round
hook.Add( "TTTEndRound", "OpenBNEndRound", function()
	for k, v in pairs( player.GetAll() ) do
		if not tobool( v:GetPData( "HasBet", false ) ) then
			net.Start( "BN_Open" )
			net.Send ( v )
		end
	end
end )

--Prep Round
hook.Add( "TTTPrepareRound", "OpenBNPrepareRound", function()
	for k, v in pairs( player.GetAll() ) do
		if not tobool( v:GetPData( "HasBet", false ) ) then
			net.Start( "BN_Open" )
			net.Send ( v )
		end
	end
end )

--Begin Round
hook.Add( "TTTBeginRound", "OpenBNBeginRound", function()
	for k, v in pairs( player.GetAll() ) do
		if not tobool( v:GetPData( "HasBet", false ) ) then
			net.Start( "BN_Open" )
			net.Send ( v )
		end
	end
end )

Clientside:


net.Receive( "BN_Open", function()
	if not BN_Opened then 
		BettingNotifier_Open()
		BN_Opened = true
	end
end )

Also is there any good way to check wether a menu is opened or not without making variables?

I’m sometimes gaining an error with this menu too:


[ERROR] addons/simple_betting_by_p4sca1/lua/autorun/client/cl_bettingsystem.lua:785: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)
  1. SetMouseInputEnabled - [C]:-1
   2. unknown - addons/simple_betting_by_p4sca1/lua/autorun/client/cl_bettingsystem.lua:785

This is the related code:


--automatically closes the panel after the preparing + BS_TimeAfterRoundStart has end
	hook.Add( "TTTBeginRound", "RemoveBPBegin", function()
		timer.Simple( BS_TimeAfterRoundStart, function()
			if BP_Opened then 
				BP:SetMouseInputEnabled( false )
				BP:SetKeyboardInputEnabled( false )
				BP:AlphaTo( 0, 2, 0, function() 
					BP:Close()
				end )
			end
		end ) 
	end )

[editline]28th August 2015[/editline]

Here’s the BettingNotifier_Open function:



function BettingNotifier_Open()
	--some important variables
	local OuterSpace	= BN_Width * 0.05
	local SpaceTopBot   = BN_Height * 0.03
	
	--only allow betting when BS_MinPlayers are connected
	if #player.GetAll() < BS_MinPlayers then return end
	
	--main betting notifier frame
	BN = vgui.Create( "DFrame" )
	BN:SetPos( ScrW() + BN_Width, BN_YPos )
	BN:SetSize( BN_Width, BN_Height )
	BN:ShowCloseButton( false )
	BN:SetTitle( "" )
	BN:SetDraggable( false )	
	BN:MoveTo(BN_XPos, BN_YPos, BN_SlideInTime, 0, 2 )
	BN:SetVisible( true )
	function BN:OnClose()
		gui.EnableScreenClicker( false )
		BN_Opened = false
	end
	function BN:Paint()
	end
	
	local BN_P = vgui.Create( "DPanel", BN )
	BN_P:SetPos( 0, 0 )
	BN_P:SetSize( BN_Width, BN_Height )
	function BN_P:Paint()
		draw.RoundedBox( 0, 0, 0, BN_Width, BN_Height, BN_BGColor )
	end
	
	--title
	local BN_P_Title = vgui.Create( "DLabel", BN_P )	
	BN_P_Title:Dock( TOP )
	BN_P_Title:DockMargin( OuterSpace, SpaceTopBot, 0, 0 )
	BN_P_Title:SetTextColor( Color( 255, 255, 255 ) )
	BN_P_Title:SetFont( "HeadingSmall" )
	BN_P_Title:SetText( "Wettsystem" )
	BN_P_Title:SizeToContents()
	
	--little tip at the bottom
	local Tip = vgui.Create( "DLabel", BN_P )
	Tip:Dock( BOTTOM )
	Tip:DockMargin( OuterSpace, 0, 0, SpaceTopBot )
	Tip:SetTextColor( Color( 255, 255, 255 ) )
	Tip:SetFont( "Tip" )
	Tip:SetText( "Tipp: Halte T, damit deine Maus sichtbar wird" )
	Tip:SizeToContents( )
	
	--button to open the betting panel
	local BN_P_W_Button = vgui.Create( "DButton", BN_P ) 
	BN_P_W_Button:Dock( FILL )
	BN_P_W_Button:DockMargin( 20, 10, 20, 10 )
	BN_P_W_Button:SetFont( "Button" )
	BN_P_W_Button:SetTextColor( Color( 255, 255, 255 ) )
	BN_P_W_Button:SetText( "Zum Wettschein" )
	BN_P_W_Button:SizeToContents()	
	BN_P_W_Button.DoClick = function()
		if not BP_Opened then
			BettingPanel_Open()
			BP_Opened = true
		end
	end
	BN_P_W_Button.Paint = function()
		draw.RoundedBox( 10, 0, 0, BN_Width - 40, BN_Height * 0.55, BN_ButtonColor )
	end
	

	local InScoreboard 
	hook.Add( "ScoreboardShow", "BSCheckScoreboard1", function()
		InScoreboard = true
	end )
	hook.Add( "ScoreboardHide", "BSCheckScoreboard12", function()
		InScoreboard = false
	end )
	--mouse cursor will be showed while holding BN_ButtonToShowMouse
	hook.Add( "Think", "BN_Input", function( )
		if BN_Opened and not InScoreboard then
			gui.EnableScreenClicker( input.IsKeyDown( BN_ButtonToShowMouse ) )
		end
	end ) 

	--automatically closes the notifier after the preparing + BS_TimeAfterRoundStart has end
	hook.Add( "TTTBeginRound", "RemoveBNBegin", function()
		timer.Simple( BS_TimeAfterRoundStart, function()
			if BN_Opened then 
				BN:AlphaTo( 0, 2, 0, function() 
					BN:Close()
				end )
			end
		end )
	end )
end	



	for k, v in pairs( player.GetAll() ) do
		if not tobool( v:GetPData( "HasBet", false ) ) then
			net.Start( "BN_Open" )
			net.Send ( v )
		end
	end


Maybe it is this not working?
Do you see any mystake, that could cause the code to only work sometimes?

[editline]29th August 2015[/editline]

I figured out when the derma menu is not showing. When the client is playing on the server and then there’s a mapchange, the notifier won’t open for this client, until there’s a restart.

This could possibly be a problem with the script not downloading and/or being run on the client. Make sure you have (in server code) AddCSLuaFile() and see if it works after that.

I have the file in lua/autorun/server. Do I still need AddCSLuaFile()?

Derma doesn’t run on the server. Derma runs on the client. You can’t open a Derma menu on the server as the server doesn’t have a screen!
Place it in lua/autorun and make sure you have


if (CLIENT) then

and


if (SERVER) then

in the appropriate places.

Edit:
AddCSLuaFile() needs to be in the server code for the client to download and run the clientside code.

No, sorry. The clientside things (the derma menu itself) is stored in lua/autorun/client.
I’m opening the menu with



net.Receive( "BN_Open", function()
	if not BN_Opened then 
		BettingNotifier_Open()
		BN_Opened = true
	end
end )


Then I have serverside code, placed in lua/autorun/server. From there, I’m doing this:



--Check when the notifier should be opened and send a net message then 
local function SendMenuOpen()
	for k, v in pairs( player.GetAll() ) do
		if not tobool( v:GetPData( "HasBet", false ) ) then
			net.Start( "BN_Open" )
			net.Send ( v )
		end
	end
end
--end round
hook.Add( "TTTEndRound", "OpenBNEndRound", SendMenuOpen )

--Prep Round
hook.Add( "TTTPrepareRound", "OpenBNPrepareRound", SendMenuOpen )

--Begin Round
hook.Add( "TTTBeginRound", "OpenBNBeginRound", SendMenuOpen )

Add a print below that serverside “if not” line; test whether the if arguments are testing correctly.
If the print shows up, it’s a problem on the client.
If the print doesn’t show up, it’s a problem with the if not line.

thanks for the advice. Will test it in about 5 hours. :slight_smile: (no time atm)

It’s fixed now. I set the PData to early, thanks for the advice! :slight_smile: