function to print players name

Quick question.


 if rand < 50
then chat.AddText( "            has survived" )
end 

thats a part of what i plan to use on a swep, to make it print the players name what function do i use?

Try ( self.Owner:GetName() … " has survived!" )


if rand < 50
then chat.AddText( self.Owner:GetName() .. " has survived!" ) 
end 

:9: attempt to index global ‘self’ (a nil value)
thats the error im getting from that, or have i placed it wrongly? I started learning lua about an hour ago so i’m SLIGHTLY new :stuck_out_tongue:

Well is it actually in a swep? Because that’s the contex I wrote it for. Do you mind expanding on what you’re trying to do/post the part of you’re code in which you need the player’s name?

self is the swep. Owner is the player holding it. GetName() return’s the name of the owner of the swep. :wink:

yeah sorry.

Im useing luapad in gmod to keep testing my lua scripts.



rand = math.random( 0, 100 )
say = "has survived"

if rand >= 50
then RunConsoleCommand "kill"
end

if rand < 50
then chat.AddText( self.Owner:GetName() .. " has survived!" ) 
end 


Thats what im using now and plan on putting it into a swep soon. Basically a russian roulette swep. 50% chance of living and dieing but when you live i want it to print in chat "(Person) has survived!

In luapad use LocalPlayer() to refer to yourself. So it would also be LocalPlayer():RunConsoleCommand(“kill”) btw.

Oh and use [lua] instead of


. :)

thankyou :D! When i’m a rich and famous lua coder i’ll give you half of what i earn :slight_smile:

Code tags are better. Don’t break the page margins and people on IE can see the code.

Ie sucks. so What the fuck

What teh fuck, don’t teach new people bullshit.



-- either
LocalPlayer():ConCommand("kill")
-- or
RunConsoleCommand("kill")


Funny that GMod uses IE as its standard browser ~