Arguments

Sorry for this noobish thread, but I’m learning Lua.

I’m confused with arguments and I’m hoping someone could explain how they work to me.
If I look at this code;


function sayMyName(whatsMyName) 
    Msg("Hey "..whatsMyName.."!
") 
end 
 
sayMyName("Jeff")

I understand it, it will print “Hey Jeff!”.
But if I look at this code;


function KeyPressed (P, key)
	Msg (P:GetName().." pressed "..key.."
")
end
 
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )

I don’t understand why it works. P and key isn’t defined like in the previous code.
So I tried making one myself, but it does not work;


function GetUserGroup( ply )
	ply:PrintMessage(HUD_PRINTTALK, ply:GetUserGroup())
end

GetUserGroup()

Can somebody explain?

Well an argument is a variable you pass into a function whenever you call it, some functions have default arguments so if you wont pass one it will use the default one.

Thats because the hook library passes the arguments through the engine.
The only way you know what a hook’s arguments are, is from the wiki.


function PlayerSpawn ( ply )
	print (ply:GetUserGroup())
end

hook.Add("PlayerSpawn","Get Rank",PlayerSpawn())

Why doesn’t this work?

Either because some other hook returns earlier or because your function is global and conflicts with another script.

So, this error;

[lua esting.lua:2] attempt to index local ‘ply’ (a nil value)
?

If you are using the code you posted above make sure it’s running server-side (lua/autorun/server/testing.lua) and in your hook.Add the third argument shouldn’t be PlayerSpawn(). Change it to just PlayerSpawn.

Or you can just use the console command:
lua_openscript lua/testing.lua

If you’re doing global functions serversided, I suggest you localize them unless you want to break apart the gamemode.

Also, I think it’s better to think of a function as a vending machine, the arguments being the buttons for your selection, and the function being executed to provide you your drink. Without the arguments, the function would be idle in most cases, but I don’t wanna confuse you.

I too had the trouble with understanding arguments even though it was quite simple.

The last paramater for hook.Add is looking for a function, not a function call. Remove the parenthesis from PlayerSpawn().

I’ll try and explain parameters to you, also. When you do function(what, ever, man) those 3 vars need to be passed into scope when you call the function. So if you did function printply( ply ), for your script to work you need a valid player object to pass to the function or else it won’t work. Example:

[lua]
function PrintPlayerInfo( pl )
if( !pl ) then return; end

 print("Player name: "..pl:GetName());

end
[/lua]

Now, this function when called and passed appropriate arguments, will print the players name. Heres an example of how to call the function.

[lua]
for _, v in pairs( player.GetAll() ) do //loop through the player table
if( ValidEntity( v ) && v:IsPlayer() ) then // check if the player is actually a player and is not nil
PrintPlayerInfo( v ); //make our function call, and pass the plaayer object as the first arg
end
end
[/lua]

I still keep getting an error. This time it’s;

TeoTown suicided!
Couldn’t dispatch user message (19)
superadmin
Couldn’t dispatch user message (19)
TeoTown suicided!
Couldn’t dispatch user message (19)
superadmin
Couldn’t dispatch user message (19)

Fixed since it is a function, you don’t call the function.

did you change the code? if so then show us it so we can help.

I used this.


 function PlayerSpawn ( ply )
	print (ply:GetUserGroup())
end

hook.Add("PlayerSpawn","Get Rank",PlayerSpawn)[\CODE]