Script i made not working correctly, no idea why.

Hello. I am making a small admin mod to help me administrate my servers but i have ran into a problem : The vgui pops up and everything, but nothing ACTUALLY happens ( ex: i select my name, and click slay, no error , but nothing happens.)

As i say, everything SHOULD work, and i proved that to myself by actually using the same method in another script ( LConsole ) and that worked.

Heres the script, MESSY CODE AHEAD !!!

[LUA]

module( ‘CGAM’ , package.seeall )

if SERVER then

local function FindPlayer( target )

	players = {}

	if not target then
		return;
	end

	for _, v in ipairs( player.GetAll()) do 
		if string.find( string.lower( v:Nick() ), string.lower( target ) ) != nil then
			table.insert( players, v )
		end
	end

	if #players >1 then
		return nil
	elseif #players <1 then
		return nil
	elseif #players == 1 then
		return players[1]
	end

end

function SendOptics( pl, Type, message )

	umsg.Start( 'CGAM.ReceiveOptics', pl )
		umsg.String( Type )
		umsg.String( message )
	umsg.End()

end

function Warn( ply , message )

	local pl = FindPlayer( ply )

	if pl then

		if not pl.Warns then pl.Warns = 0 end

		pl.Warns = pl.Warns + 1

		SendOptics( pl , 'Warn' , message )

	end
	
end

concommand.Add("CGAM_Warn", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]

	if pl:IsAdmin() then

		Warn( PLid , message )

	end

end)

function Slay( ply )

	local pl = FindPlayer( ply )

	if pl then

		pl:Kill()
		SendOptics( pl, 'Slay', "You got slayed by an admin." )
		
	end
	
end

concommand.Add("CGAM_Slay", function( pl , cmd, args )
	local Plid = args[1]

	if pl:IsAdmin() then

		Slay( PLid )

	end

end)

function Kick( ply, message )

	local pl = FindPlayer( ply )

	if pl then
	
		if message == '< Optional >' then

			pl:Kick( 'Old and useless' )

		else

			pl:Kick( message )

		end
	
	end

end

concommand.Add("CGAM_Kick", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]

	if pl:IsAdmin() then

		Kick( PLid , message )

	end

end)

function Ban( ply, message, time )

	local pl = FindPlayer( ply )

	if pl then

		if message == '< Optional >' then

			pl:Ban( time, 'Old and useless' )

		else

			pl:Ban( time, message )

		end
	
	end

end

concommand.Add("CGAM_Ban", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]
	local time = args[3]

	if pl:IsAdmin() then

		Ban( PLid , message, time )

	end

end)

end

if CLIENT then

local function ReceiveOptics( um )

	local Type = um:ReadString()
	local Message = um:ReadString()
	
	if Type == 'Warn' then
		chat.AddText(
			Color(0,0,255) , 'CGAM' ,
			Color(255,255,255), " : ",
			Color(255,255,255), "You got a warning : ",
			Color(255,255,255), Message)
		chat.PlaySound()
	elseif Type == 'Slay' then
		chat.AddText(
			Color(0,0,255) , 'CGAM' ,
			Color(255,255,255), " : ",
			Color(255,255,255), Message)
		chat.PlaySound()
	end

end

usermessage.Hook( 'CGAM.ReceiveOptics', ReceiveOptics )

function OpenUI()

	local DLabel2
	local DNumSlider1
	local DTextEntry3
	local DLabel1
	local DButton5
	local DButton4
	local DButton3
	local DButton2
	local DComboBox1
	local DPanel1
	local DFrame1

	DFrame1 = vgui.Create('DFrame')
	DFrame1:SetSize(418, 172)
	DFrame1:Center()
	DFrame1:SetTitle('CG Admin Mod')
	DFrame1:SetSizable(true)
	DFrame1:SetDeleteOnClose(false)
	DFrame1:MakePopup()

	DPanel1 = vgui.Create('DPanel')
	DPanel1:SetParent(DFrame1)
	DPanel1:SetSize(413, 145)
	DPanel1:SetPos(2, 24)
	
	DLabel1 = vgui.Create('DLabel')
	DLabel1:SetParent(DPanel1)
	DLabel1:SetPos(128, 45)
	DLabel1:SetText('Reason :')
	DLabel1:SizeToContents()
	
	DLabel2 = vgui.Create('DLabel')
	DLabel2:SetParent(DPanel1)
	DLabel2:SetPos(131, 15)
	DLabel2:SetText('CG Admin Mod made by Ningaglio and the CG Dev Team')
	DLabel2:SizeToContents()
	
	DTextEntry3 = vgui.Create('DTextEntry') // Reason
	DTextEntry3:SetParent(DPanel1)
	DTextEntry3:SetSize(229, 20)
	DTextEntry3:SetPos(178, 42)
	DTextEntry3:SetText('< Optional >')
	DTextEntry3.OnEnter = function()
	end

	DNumSlider1 = vgui.Create('DNumSlider') // Time
	DNumSlider1:SetSize(284, 40)
	DNumSlider1:SetParent(DPanel1)
	DNumSlider1:SetPos(125, 68)
	DNumSlider1:SetDecimals(0)
	DNumSlider1.OnMouseReleased = function()
	end
	DNumSlider1.OnValueChanged = function()
	end
	DNumSlider1:SetText('Ban Time')
	DNumSlider1:SetValue(0)
	DNumSlider1:SetMinMax( 0, 9999)

	DComboBox1 = vgui.Create('DComboBox')
	DComboBox1:SetParent(DPanel1)
	DComboBox1:SetSize(117, 138)
	DComboBox1:SetPos(2, 3)
	DComboBox1:EnableHorizontal(false)
	DComboBox1:EnableVerticalScrollbar(true)
	DComboBox1.OnMousePressed = function()
	
	end
	DComboBox1:SetMultiple(false)
	
	for k,v in pairs( player.GetAll() ) do
		DComboBox1:AddItem( v:Nick() )
	end
	

	DButton2 = vgui.Create('DButton') // Warn( Pid , Message )
	DButton2:SetParent(DPanel1)
	DButton2:SetSize(70, 25)
	DButton2:SetPos(123, 115)
	DButton2:SetText('Warn')
	DButton2.DoClick = function()

		RunConsoleCommand( "CGAM_Warn", DComboBox1:GetSelected() , DTextEntry3:GetValue() )
	
	end

	DButton3 = vgui.Create('DButton') // Slay( Pid )
	DButton3:SetParent(DPanel1)
	DButton3:SetSize(70, 25)
	DButton3:SetPos(195, 115)
	DButton3:SetText('Slay')
	DButton3.DoClick = function()

		RunConsoleCommand( "CGAM_Slay", DComboBox1:GetSelected() )
	
	end

	DButton4 = vgui.Create('DButton') // Kick( Pid, message )
	DButton4:SetParent(DPanel1)
	DButton4:SetSize(70, 25)
	DButton4:SetPos(267, 115)
	DButton4:SetText('Kick')
	DButton4.DoClick = function()

		RunConsoleCommand( "CGAM_Kick", DComboBox1:GetSelected() , DTextEntry3:GetValue() )
	
	end

	DButton5 = vgui.Create('DButton') // Ban( Pid, Message, Time )
	DButton5:SetParent(DPanel1)
	DButton5:SetSize(70, 25)
	DButton5:SetPos(339, 115)
	DButton5:SetText('Ban')
	DButton5.DoClick = function()

		RunConsoleCommand( "CGAM_Ban", DComboBox1:GetSelected() , DTextEntry3:GetValue(), DNumSlider1:GetValue() )
	
	end
	
end

concommand.Add("CGAM_OpenMenu", OpenUI )

end
[/LUA]

and this is what its supposed to look like :

http://www.citrongamers.com/server/gm_construct0013.jpg

I had this problem as well, the strings you are entering into the combo box are just entries, not the actual player entity, but that is where I got no more help, so, just wait for someone who knows how to add player entities to the combo box.

I am transforming everything i receive with the function FindPlayer or something.

Any errors?

When I am debugging console commands, I put several ‘checkpoints’ into the code, those being simple print() messages. If it prints into the console, you know it gets past a certain ‘if’ or runs a certain function.

Well, so far i know that on the server side functions ( like warn and stuff ) the if pl then is the thing braking it, but otherwise it brakes anyways D: Please help though

Debugging time! I’m fairly certain the issue is clientside, so put

[lua]print(DComboBox1:GetSelected())[/lua] in one of your DoClick functions.

nope, its serverside, i remove if pl then on the Warn function and i get this error, no reason why i should get it.




lua\autorun\adminmod.lua:43: attempt to index local 'pl' (a nil value)



[editline]11:32AM[/editline]

error is in blue

[editline]11:34AM[/editline]

@Dave : I used your code, put it in OnMousePressed for the DComboBox, pirinted nothing o.0

[editline]11:36AM[/editline]

wiki.garrysmod.com down?

[editline]11:36AM[/editline]

back up phew

Well, throughout the code, when using the function “Warn(ply, message)”, you are using, for example on line 60:
[lua]
Warn(PLid, mmessage)
[/lua]
when the variable you have defined before this is called “Plid” (no capital “L”).
That is the first error that is causing the message you are getting.
Try fixing that and posting any more errors you get.

Do what ZeMaster said, and change the following things:

[lua]for _, v in ipairs( player.GetAll()) do
if string.Trim( v:Nick() ) == string.Trim( target ) then
table.insert( players, v )
end
end
[/lua]

You can use this in your FindPlayers function as you are populating your DComboBox automatically. It will work just as well.

[lua]
RunConsoleCommand( “CGAM_Warn”, DComboBox1:GetSelected():GetValue() , DTextEntry3:GetValue() )
[/lua]

You want to use DComboBox1:GetSelected():GetValue(), else you will not get the name of the player, but the panel of the ComboBox that is currently selected.

Used this on the buttong:

print( 'Pressed Warn on ’ … tostring(DComboBox1:GetItems()[1]) )

prints to the console :

Pressed Warn on Panel: 03362DB8

-snip-

Won’t work, look up ^

okay, doing what you said

[editline]12:06PM[/editline]

@Nonsense

No, the only value in GetSelectedItems() is at index 1 if SetMultiple is false.



1    =    Panel: 25940768


Like I said, use GetSelectedItems()[1]:GetValue() or GetSelected():GetValue().

Ah, of course, was thinking of a problem like this I had a few months ago, I fixed it by doing that.

errrmm … not to disencourage you all, but its still not working. This is the code i have:

[LUA]

module( ‘CGAM’ , package.seeall )

if SERVER then

local function FindPlayer( target )

	players = {}

	if not target then
		return;
	end

	for _, v in ipairs( player.GetAll()) do 
		if string.find( string.lower( v:Nick() ), string.lower( target ) ) != nil then
			table.insert( players, v )
		end
	end

	if #players >1 then
		return nil
	elseif #players <1 then
		return nil
	elseif #players == 1 then
		return players[1]
	end

end

function SendOptics( pl, Type, message )

	umsg.Start( 'CGAM.ReceiveOptics', pl )
		umsg.String( Type )
		umsg.String( message )
	umsg.End()

end

function Warn( ply , message )

	local pl = FindPlayer( ply )

	if pl then

		if not pl.Warns then pl.Warns = 0 end

		pl.Warns = pl.Warns + 1

		SendOptics( pl , 'Warn' , message )
		

	end
	
end

concommand.Add("CGAM_Warn", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]

	if pl:IsAdmin() then

		Warn( Plid , message )

	end

end)

function Slay( ply )

	local pl = FindPlayer( ply )

	if pl then

		pl:Kill()
		SendOptics( pl, 'Slay', "You got slayed by an admin." )
		
	end
	
end

concommand.Add("CGAM_Slay", function( pl , cmd, args )
	local Plid = args[1]

	if pl:IsAdmin() then

		Slay( Plid )

	end

end)

function Kick( ply, message )

	local pl = FindPlayer( ply )

	if pl then
	
		if message == '< Optional >' then

			pl:Kick( 'Old and useless' )

		else

			pl:Kick( message )

		end
	
	end

end

concommand.Add("CGAM_Kick", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]

	if pl:IsAdmin() then

		Kick( Plid , message )

	end

end)

function Ban( ply, message, time )

	local pl = FindPlayer( ply )

	if pl then

		if message == '< Optional >' then

			pl:Ban( time, 'Old and useless' )

		else

			pl:Ban( time, message )

		end
	
	end

end

concommand.Add("CGAM_Ban", function( pl , cmd, args )
	local Plid = args[1]
	local message = args[2]
	local time = args[3]

	if pl:IsAdmin() then

		Ban( Plid , message, time )

	end

end)

end

if CLIENT then

local function ReceiveOptics( um )

	local Type = um:ReadString()
	local Message = um:ReadString()
	
	if Type == 'Warn' then
		chat.AddText(
			Color(0,0,255) , 'CGAM' ,
			Color(255,255,255), " : ",
			Color(255,255,255), "You got a warning : ",
			Color(255,255,255), Message)
		chat.PlaySound()
	elseif Type == 'Slay' then
		chat.AddText(
			Color(0,0,255) , 'CGAM' ,
			Color(255,255,255), " : ",
			Color(255,255,255), Message)
		chat.PlaySound()
	end

end

usermessage.Hook( 'CGAM.ReceiveOptics', ReceiveOptics )

function OpenUI()

	local DLabel2
	local DNumSlider1
	local DTextEntry3
	local DLabel1
	local DButton5
	local DButton4
	local DButton3
	local DButton2
	local DComboBox1
	local DPanel1
	local DFrame1

	DFrame1 = vgui.Create('DFrame')
	DFrame1:SetSize(418, 172)
	DFrame1:Center()
	DFrame1:SetTitle('CG Admin Mod')
	DFrame1:SetSizable(false)
	DFrame1:SetDeleteOnClose(false)
	DFrame1:MakePopup()

	DPanel1 = vgui.Create('DPanel')
	DPanel1:SetParent(DFrame1)
	DPanel1:SetSize(413, 145)
	DPanel1:SetPos(2, 24)
	
	DLabel1 = vgui.Create('DLabel')
	DLabel1:SetParent(DPanel1)
	DLabel1:SetPos(128, 45)
	DLabel1:SetText('Reason :')
	DLabel1:SizeToContents()
	
	DLabel2 = vgui.Create('DLabel')
	DLabel2:SetParent(DPanel1)
	DLabel2:SetPos(131, 15)
	DLabel2:SetText('CG Admin Mod made by Ningaglio and the CG Dev Team')
	DLabel2:SizeToContents()
	
	DTextEntry3 = vgui.Create('DTextEntry') // Reason
	DTextEntry3:SetParent(DPanel1)
	DTextEntry3:SetSize(229, 20)
	DTextEntry3:SetPos(178, 42)
	DTextEntry3:SetText('< Optional >')
	DTextEntry3.OnEnter = function()
	end

	DNumSlider1 = vgui.Create('DNumSlider') // Time
	DNumSlider1:SetSize(284, 40)
	DNumSlider1:SetParent(DPanel1)
	DNumSlider1:SetPos(125, 68)
	DNumSlider1:SetDecimals(0)
	DNumSlider1.OnMouseReleased = function()
	end
	DNumSlider1.OnValueChanged = function()
	end
	DNumSlider1:SetText('Ban Time')
	DNumSlider1:SetValue(0)
	DNumSlider1:SetMinMax( 0, 9999)

	DComboBox1 = vgui.Create('DComboBox')
	DComboBox1:SetParent(DPanel1)
	DComboBox1:SetSize(117, 138)
	DComboBox1:SetPos(2, 3)
	DComboBox1:EnableHorizontal(false)
	DComboBox1:EnableVerticalScrollbar(true)
	DComboBox1.OnMousePressed = function()
	
		print(DComboBox1:GetSelected())
	
	end
	DComboBox1:SetMultiple(false)
	
	for k,v in pairs( player.GetAll() ) do
		DComboBox1:AddItem( v:Nick() )
	end
	

	DButton2 = vgui.Create('DButton') // Warn( Pid , Message )
	DButton2:SetParent(DPanel1)
	DButton2:SetSize(70, 25)
	DButton2:SetPos(123, 115)
	DButton2:SetText('Warn')
	DButton2.DoClick = function()

		RunConsoleCommand( "CGAM_Warn", DComboBox1:GetSelectedItems()[1]:GetValue() , DTextEntry3:GetValue() )
	
	end

	DButton3 = vgui.Create('DButton') // Slay( Pid )
	DButton3:SetParent(DPanel1)
	DButton3:SetSize(70, 25)
	DButton3:SetPos(195, 115)
	DButton3:SetText('Slay')
	DButton3.DoClick = function()

		RunConsoleCommand( "CGAM_Slay", DComboBox1:GetSelectedItems()[1]:GetValue() )
	
	end

	DButton4 = vgui.Create('DButton') // Kick( Pid, message )
	DButton4:SetParent(DPanel1)
	DButton4:SetSize(70, 25)
	DButton4:SetPos(267, 115)
	DButton4:SetText('Kick')
	DButton4.DoClick = function()

		RunConsoleCommand( "CGAM_Kick", DComboBox1:GetSelected():GetValue() , DTextEntry3:GetValue() )
	
	end

	DButton5 = vgui.Create('DButton') // Ban( Pid, Message, Time )
	DButton5:SetParent(DPanel1)
	DButton5:SetSize(70, 25)
	DButton5:SetPos(339, 115)
	DButton5:SetText('Ban')
	DButton5.DoClick = function()

		RunConsoleCommand( "CGAM_Ban", DComboBox1:GetSelected():GetValue() , DTextEntry3:GetValue(), DNumSlider1:GetValue() )
		
	end
	
end

concommand.Add("CGAM_OpenMenu", OpenUI )

end

[/LUA]

Works now! thank you all. If anyone needs the code, please just take it :slight_smile: