• Need help on a simple lua code
    12 replies, posted
Hi, im new at coding in lua and i have a little issue with my code : [CODE]function suicidio (victim, attacker) if ( victim == attacker ) then PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) else PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." ) end end hook.Add("GM:PlayerDeath", "muerte", suicidio( victim, attacker ) end )[/CODE] I would like to know what is wrong on this code :) The error is : [CODE][ERROR] lua/autorun/cosas.lua:15: '=' expected near 'function'[/CODE]
I usually connect the function name with its arguments, but I'm not sure if that does anything [lua]function suicidio(victim, attacker)[/lua]
Now the code is [CODE]function suicidio(victim, attacker) if ( victim == attacker ) then PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) else PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." ) end end hook.Add("GM:PlayerDeath", "muerte", suicidio( victim, attacker ) end )[/CODE] and it still gives the same error :(
What does the last ) & end close?
It ends hook.Add(
You don't have to end a hook.Add with ''end''
Okay the problem was that end is not needed. The code now is : [CODE] function suicidio(victim,attacker) if ( victim == attacker ) then PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) else PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." ) end end hook.Add("GM:PlayerDeath", "muerte", suicidio( victim, attacker ) ) [/CODE] But the error now is [ERROR] lua/autorun/cosas.lua:4: attempt to index local 'victim' (a nil value)
GM:PlayerDeath uses 3 arguments, victim,inflictor,attacker
[QUOTE=jonitah112;49210805]Okay the problem was that end is not needed. The code now is : [CODE] function suicidio(victim,attacker) if ( victim == attacker ) then PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) else PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." ) end end hook.Add("GM:PlayerDeath", "muerte", suicidio( victim, attacker ) ) [/CODE] But the error now is [ERROR] lua/autorun/cosas.lua:4: attempt to index local 'victim' (a nil value)[/QUOTE] Get rid of the GM: in hook.Add you only need to have PlayerDeath. Also you don't need the brackets with the arguments in them when calling a function from hook.Add. The arguments are automatically passed. This is the proper way. [CODE]hook.Add("PlayerDeath", "muerte", suicidio )[/CODE]
[QUOTE=boxvader;49210860]Get rid of the GM: in hook.Add you only need to have PlayerDeath. Also you don't need the brackets with the arguments in them when calling a function from hook.Add. The arguments are automatically passed. This is the proper way. [CODE]hook.Add("PlayerDeath", "muerte", suicidio )[/CODE][/QUOTE] Great man! Thank you :D This is my very first code :) i'm glad it finally works! :D
No problem glad you finally got it to work. I would suggest that you mark this thread as solved now.
Where do i mark it as solved? :o
[QUOTE=jonitah112;49211045]Where do i mark it as solved? :o[/QUOTE] Top of the page, left hand side I think
Sorry, you need to Log In to post a reply to this thread.