Functions

Can somone please explain functions to me, im trying to learn lua but failing.

[lua]function playername(ply)
ply:ChatPrint(ply:Name())
end
[/lua]

I understand you call the function using playername() but dont understand ply bit, and i see it used frequently when looking at existing lua. When i run the script calling it using playername() it gives me this error
:2: attempt to index local ‘ply’ (a nil value).

ply is an argument that is passed to the function every time it is called, For example, in this code i am passing a different argument to the function each time i call it:


function Add(a, b)
	print(a + b)
end

Add(1, 5)   // Output: 6
Add(10, 4)	// Output: 14
Add(1, 1)	// Output: 2

In your case, you are not passing an argument to the function so it is nil.

[editline]04:54PM[/editline]

As you can probably tell, i am ot very good at explaining. Read this, hopefully it will enlighten you.

But if i do
[lua]function playername(ply)
ply:ChatPrint(ply:Name())
end

playername(Jake)[/lua]

it still gives me an error

:2: attempt to index local ‘ply’ (a nil value)

Sorry :stuck_out_tongue: I’m just not that clever cries

edit

if you could could you please show me an example using that function?

ply should be a player object, not a player name. If this is Clientside script you can get your own player object with LocalPlayer().

For example:


function playername(ply)
   ply:ChatPrint(ply:Name())
end
 

playername(LocalPlayer())

oh wow thanks, final question. What would i replace LocalPlayer() with to get somone elses name?

[lua]for k , v in ipairs(player.GetAll()) do
if string.find(v:Nick() , “Jimbob” ) then
playername(v)
end
end[/lua]

You are using the player’s name to find the player’s name, that is completly useless.

@jaykc34: It depends entirely on the context, are you making this for a SWEP, concommand etc?

swep

Then you need a trace, for example:


function SWEP.PrimaryAttack()
	local tr = self.Owner:GetEyeTrace()
	if ValidEntity(tr.Entity) and tr.Entity:IsPlayer() then
		playername(tr.Entity)
	end
end

That is a very basic example.

[editline]05:25PM[/editline]

In fact this would work better (assuming you want their name to be printed on your screen)


function SWEP.PrimaryAttack()
	local tr = self.Owner:GetEyeTrace()
	if ValidEntity(tr.Entity) and tr.Entity:IsPlayer() then
		self.Owner:ChatPrint(tr.Entity:Nick())
	end
end

[lua]function playername(ply)
ply:ChatPrint(ply:Name())
end

rand = math.random( 0,6 )

if rand == 3
then RunConsoleCommand( “kill” )
else chat.AddText( playername(LocalPlayer()) … “has survived!”)
end[/lua]

What is wrong with that? I’m getting this error.

:9: attempt to concatenate a nil value

Chatprint prints the name to the screen, what you are looking for is this:


function playername(ply)
   return ply:Name()
end

local rand = math.random( 0,6 )

if rand == 3 then
	RunConsoleCommand( "kill" )
else
	chat.AddText( playername(LocalPlayer()) .. "has survived!")
end

Thankyou :slight_smile: Much love