Nil function bypass?

Hi, i am making a hitmenu hud for the hitman, and i have a timer that keeps refreshing the hitmenu every second to show the hits. BUT if you become a hitman, and there is no hits, it sais there is script errors and my function “refreshMenu()” is nil. is there a way to bypass this so it wont say errors?



	victim = vgui.Create("DScrollPanel", list);
	victim:Dock(FILL);

	top = 0;

	for k, v in pairs(RP_HITLIST) do
		if ( IsValid(k) ) then
			if (top < v) then
				top = v;
			end;
		end;
	end;

	for k, v in pairs(RP_HITLIST) do
		if ( IsValid(k) ) then
			panel = victim:Add("DPanel");


function refreshMenu()
  victim:AddItem(panel);
  end;


net.Receive("cn_hitmanList", function(ply)
	timer.Simple(1,function()
		 if (LocalPlayer():Team() == TEAM_HITMAN) then
   createHitList()
   timer.Create( "hitmenu", 1, 0, function()
   refreshMenu()
   end)
   else
   closeList()
  end
	end)
end)

It works fine if there is a hit already when you switch jobs but i still need help!

Looks like…you bypassed a nil function.

No Because if i become a hitman while there are no hits, it sais lua error but the script still works. If i become a hitman while there is a hit, then it works. The 3rd box of code is that happends when you change teams. Is there a line a code similar to VB like “On Error resume Next”?

Well, for your specific system, I’d go ahead and start by looking at the new GMod wiki. These little bits of code are useless to me.

okay sorry,

c+p your lua error here, obviously you have no idea what your talking about cause you called nil a function

It isnt nil when a player places a hit though. i know what im talking about, but i admit im not good at explaining my thinking. it will not drop an error IF there is already a hit. i just need my script to ignore it being nil if there is no hits. Also, Line 178 to 182 is in the 3rd code box on the top post.


[ERROR] gamemodes/darkrp/gamemode/modules/hitman/cl_init.lua:182: attempt to call global 'refreshMenu' (a nil value)
  1. unknown - gamemodes/darkrp/gamemode/modules/hitman/cl_init.lua:182

Timer Failed! [hitmenu][@gamemodes/darkrp/gamemode/modules/hitman/cl_init.lua (line 178)]


Timer is in box 3,

Heres the rest of my code attached to the function


victim = vgui.Create("DScrollPanel", list);
	victim:Dock(FILL);

	top = 0;

	for k, v in pairs(RP_HITLIST) do
		if ( IsValid(k) ) then
			if (top < v) then
				top = v;
			end;
		end;
	end;

	for k, v in pairs(RP_HITLIST) do
		if ( IsValid(k) ) then
			panel = victim:Add("DPanel");
			panel:Dock(TOP);
			panel:DockMargin(0, 0, 0, 4);
			panel:SetTall(36);

			name = vgui.Create("DLabel", panel);
			name:SetText( k:Name() );
			name:SetFont("cn_Medium");
			name:SetContentAlignment(4);
			name:SetPos(40, 8);
			name:SetTextColor( Color(125, 125, 125) );
			name:SetWide( (width * 0.66) - 40 );

			amount = v;
			value = (amount / top) * 255;

			pay = panel:Add("DLabel");
			pay:SetText("$"..amount);
			pay:SetFont("DermaDefaultBold");
			pay:SetContentAlignment(6);
			pay:Dock(RIGHT);
			pay:DockMargin(5, 5, 5, 5)
			pay:SetTextColor( Color(255 - value, value, 25) );
			pay:SetExpensiveShadow(1, color_black);

			avatar = vgui.Create("AvatarImage", panel);
			avatar:SetPos(2, 2);
			avatar:SetSize(32, 32);
			avatar:SetPlayer(k);

			victim:AddItem(panel);
   
  function refreshMenu()
  victim:AddItem(panel);
  end;

		end;
	end;

this is a bad way to make a hit creation system even if it is just derma, check out the most recent darkrp hitman system and see how that works

Ughhh im not asking you to evaluate my system. im simply asking for help on my system.

I won’t tell you how to improve your code because you seem set on using it, but to fix your error you could do this…



net.Receive("cn_hitmanList", function(ply)
	timer.Simple(1,function()
		 if (LocalPlayer():Team() == TEAM_HITMAN) then
			createHitList()
			timer.Create( "hitmenu", 1, 0, function()
				if refreshMenu then
					refreshMenu()
				else
					//If the function refreshMenu is nil then you will run the code here instead of calling the function.
				end
			end)
		else
			closeList()
		end
	end)
end)


or you could do this



victim = vgui.Create("DScrollPanel", list);
victim:Dock(FILL);

function refreshMenu()
	//All were doing is creating a default function. So if there arent any htis and you call refreshMenu() it runs this function.
	//If there are people with hits on them then this function will get overwritten.
end

top = 0;
for k, v in pairs(RP_HITLIST) do
	if ( IsValid(k) ) then
		if (top < v) then
			top = v;
		end;
	end;
end;

for k, v in pairs(RP_HITLIST) do
	if ( IsValid(k) ) then
		panel = victim:Add("DPanel");
		panel:Dock(TOP);
		panel:DockMargin(0, 0, 0, 4);
		panel:SetTall(36);

		name = vgui.Create("DLabel", panel);
		name:SetText( k:Name() );
		name:SetFont("cn_Medium");
		name:SetContentAlignment(4);
		name:SetPos(40, 8);
		name:SetTextColor( Color(125, 125, 125) );
		name:SetWide( (width * 0.66) - 40 );

		amount = v;
		value = (amount / top) * 255;

		pay = panel:Add("DLabel");
		pay:SetText("$"..amount);
		pay:SetFont("DermaDefaultBold");
		pay:SetContentAlignment(6);
		pay:Dock(RIGHT);
		pay:DockMargin(5, 5, 5, 5)
		pay:SetTextColor( Color(255 - value, value, 25) );
		pay:SetExpensiveShadow(1, color_black);

		avatar = vgui.Create("AvatarImage", panel);
		avatar:SetPos(2, 2);
		avatar:SetSize(32, 32);
		avatar:SetPlayer(k);

		victim:AddItem(panel);
   
		function refreshMenu()
			victim:AddItem(panel);
		end;
	end;
end;


That should point you in the right direction, if you need any more help just post back.

He wants us to help without evaluating what he needs help with.

Thank you obliviator, and :v: parker :stuck_out_tongue:

Boy you are full of awful posts at the moment.

It’s not a crime to evaluate something people are asking for help with, it might actually solve more problems down the line if you point out that their code is flawed in more ways than they think or tell them of more effective methods of doing things.

Getting hostile about people giving you more options is just stupid.