• Functions
    11 replies, posted
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: [code]function Add(a, b) print(a + b) end Add(1, 5) // Output: 6 Add(10, 4) // Output: 14 Add(1, 1) // Output: 2[/code] 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 [url=http://www.lua.org/manual/5.1/]this[/url], hopefully it will enlighten you.
[QUOTE=MakeR;16646642]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: [code]function Add(a, b) print(a + b) end Add(1, 5) // Output: 6 Add(10, 4) // Output: 14 Add(1, 1) // Output: 2[/code] In your case, you are not passing an argument to the function so it is nil.[/QUOTE] 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 :P 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 [i]LocalPlayer()[/i]. For example: [code]function playername(ply) ply:ChatPrint(ply:Name()) end playername(LocalPlayer())[/code]
[QUOTE=MakeR;16646776]ply should be a player object, not a player name. If this is Clientside script you can get your own player object with [i]LocalPlayer()[/i]. For example: [code]function playername(ply) ply:ChatPrint(ply:Name()) end playername(LocalPlayer())[/code][/QUOTE] 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]
[QUOTE=Retirsch;16646854][lua]for k , v in ipairs(player.GetAll()) do if string.find(v:Nick() , "Jimbob" ) then playername(v) end end[/lua][/QUOTE] 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?
[QUOTE=MakeR;16646881]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?[/QUOTE] swep
Then you need a trace, for example: [code]function SWEP.PrimaryAttack() local tr = self.Owner:GetEyeTrace() if ValidEntity(tr.Entity) and tr.Entity:IsPlayer() then playername(tr.Entity) end end[/code] 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) [code]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[/code]
[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: [code]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[/code]
[QUOTE=MakeR;16647286]Chatprint prints the name to the screen, what you are looking for is this: [code]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[/code][/QUOTE] Thankyou :) Much love
Sorry, you need to Log In to post a reply to this thread.