Make function run on game startup?

So. For learning porpuses I am trying to make a Admin addon thing. How do I make it so a certain function runs when the game starts up. The function will just add a bunch of console command including a command to open the menu.

What do you mean by on game startup?

When the game first opens, in the menu? Not sure.

When you first connect to a server? Don’t wrap it in a hook, or use Initialize.

When you spawn? PlayerInitialSpawn hook.

When the player entity comes to exist? InitPostEntity hook.

I mean the first time the player joins the server

The first time EVER, or just any time?

Do you want it to trigger right when they click the connect button? game-event player_connect will do that, in addition to the new CheckPassword hook.

Do you want it to trigger when their player spawns but their entity may still be invalid? PlayerInitialSpawn.

When fully loaded? Custom hook, or use InitPostEntity.

You don’t need to add console commands when they spawn, just add them to your code and check if they are the right rank to use them, you don’t need to add them individually for people.

I tried what Acecool said but it doesnt work. Please keep in mind that I am trying to make something like AssMod or etc.

Maybe the code is in the wrong section? Here is the directory of the file… addons/playeradmin/lua/init.lua

addons/playeradmin/lua/autorun/server/init.lua for server stuff
addons/playeradmin/lua/autorun/client/init.lua for clientstuff
addons/playeradmin/lua/autorun/init.lua for shared stuff

Note, in AUTORUN, you do NOT need to use Includes or AddCSLuaFile( ); its done for you.

If you stray, such as making a nested folder in server, client or / which don’t look like the above path then you will need to include/addcslua.

Here’s how I load map data from one addon:

[lua] local _folder = “autorun/server/maps/”;
local _file = string.lower( game.GetMap( ) ) … “.lua”;
if ( !file.Exists( _folder … _file, “LUA” ) ) then ErrorNoHalt( "SCar and WAC Entity Spawner : File not found : " … _folder … _file … "
" ); return; end
include( _folder … _file );

MsgC( Color( 0, 255, 0, 255 ), "Acecools Entity Spanwer for SCars and WAC loaded: " .. _folder .. _file .. "

" );[/lua]

Loads this: addons/acecool/lua/autorun/server/maps/<map_name>.lua

You can read and load files recursively easily. Here’s the skeleton game-mode I’m showing until I open up the svn which has a newer loader with more features, less repeats plus more helper functions:

Alright. Inside addons/playeradmin/lua/autorun/init.lua I put this code.

local function pa_MenuOpen()
	if(CLIENT) then
		panel = vgui.Create("DFrame")
		panel:SetSize(450, 600)
		panel:SetPos(ScrW() / 2, ScrH() / 2)
		//panel:SetPos(0, 0)
		panel:SetTitle("Player Admin Menu")
		local kick = vgui.Create("DButton")
		kick:SetSize(100, 50)
		kick.DoClick = function( button )
concommand.Add("+pa_Menu", pa_MenuOpen)

local function pa_MenuClose()
	panel:SetVisible(false) (<- this is line 43)
concommand.Add("-pa_Menu", pa_MenuClose)

and all I get in console is this.

[ERROR] addons/playeradmin/lua/autorun/init.lua:43: attempt to index global 'panel' (a nil value)
  1. unknown - addons/playeradmin/lua/autorun/init.lua:43
   2. unknown - lua/includes/modules/concommand.lua:69

and aside from that nothing happens at all. I tried to do research but I am lost.

Localise panel.

Still get the exact same thing.

If you localised it you should be getting a different error.

I did this

local panel = vgui.Create("DFrame")

is this what you meant?

You’re getting the error because you put the + concommand in CLIENT, but the - concommand is SERVER and CLIENT.

Move the code to addons/playeradmin/lua/autorun/client/blah.lua

OR add if ( SERVER ) then return; end at the TOP of the file.

For the SERVER options, you’ll need to network the KICK to the server because the server will have no way of knowing that the button was clicked… vgui doesn’t exist on the server. Here are some ways networking works:

Make sure the SERVER verifies the player in the net.Receive to ensure they have the rights to kick a player. Don’t trust user-input.

Thanks I got stuff working :smiley:

ONE last question though

on client I have this:

function getTeam( player )
	if(!player:IsAdmin() || !player:IsSuperAdmin()) then
		return "Player"
	if(player:IsAdmin()) then
		return "Admin"
	if(player:IsSuperAdmin()) then
		return "Super Admin"

local function pa_Menu()
	if(getTeam(LocalPlayer()) != "Player") then
		//LocalPlayer():SendHint("Not a admin/super", 2)
		local panel = vgui.Create("DFrame")
		panel:SetSize(450, 600)
		panel:SetPos(ScrW() / 2 - 450 / 2, ScrH() / 2 - 600 / 2)
		//panel:SetPos(0, 0)
		panel:SetTitle("Player Admin Menu")
		local pplTable = vgui.Create("DListView")
		pplTable:SetPos(6, 26)
		pplTable:SetSize(440, 250)
		for k,v in pairs(player.GetAll()) do
		//local kick = vgui.Create("DButton")
		//kick:SetSize(100, 50)
		//kick.DoClick = function( button )
		//	  Player(1):Kick(">:D")
concommand.Add("pa_Menu", pa_Menu)

and on server I have this:

util.AddNetworkString( "Test2" )
net.Receive( "Test2", function()    
    Player(1):ConCommand("say HEY!")

Not sure what the question is, but I see an issue with your logic:
if(!player:IsAdmin() || !player:IsSuperAdmin()) then
return “Player”

Look at it.

If the player is not admin, OR not super admin then return player.

That logic will return player unless the player is a super admin ( whereby the player will also be an admin ).

Logic table ( We don’t need to do SA not A because it should never happen ):

!A || !SA
T F = T = NOT as designed == Admins will be blocked.
T T = F = As designed
F F = T = As designed

To fix that, you’d inverse the logic…

Change all NOTs to IS ( remove ! ), change all OR to &&…


A && SA… Then it becomes more clear why it fails for player if you’re trying to get admin… This will only work for SA… A good strategy if your logic isn’t working, is inverse the logic and see if it still makes sense ( remember the return would change too ).

Any time you do a POWER LEVEL style of hierarchy with ifs, lets say you want to check for highest number…

You don’t do if x >= y AND x <= z…

It is easier to do:

if x >= 10

elseif x >= 6

elseif x >= 2


You are doing it in reverse…

[lua]// Check highest power first.
local ENUM_SA = 1;
local ENUM_A = 2;
local ENUM_P = 3;
function getTeam( _p )
if ( _p:IsSuperAdmin( ) ) then
return ENUM_SA;
elseif ( _p:IsAdmin( ) ) then
return ENUM_A;

return ENUM_P;


I did Enum just for the sake of using that; checking a number is less expensive that string comparisons.

Well thanks for the logic help. But I forget to mention the question XD

I get the error:
[ERROR] addons/playeradmin/lua/autorun/init.lua:96: attempt to call method ‘ConCommand’ (a nil value)

And plus I know I am doing it in reverse. I am doing it like this so I could test what happens if not staff

What is Player( 1 ), that part appears to be a typo and should be Entity( 1 ):ConCommand( )

Player(#) is a function to return the player based off their user ID. Your userID might be wrong, thus leading to an error because you’re trying to make a non-existent player run a console command.

You can type status in console to check your user IDs.

Also, the function adds 1 to the user ID, so you should try Player(0) to get the first player.

FYI, Player( 1 ) != Entity( 1 )

You should never use a static number in those functions in your code. Only for debugging in single player or listen server, using Entity( 1 ).