• Print

Author Topic: Quick question on if statements  (Read 12651 times)

0 Members and 1 Guest are viewing this topic.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Quick question on if statements
« on: October 30, 2015, 03:11:31 pm »
Pretty much what the title says, how would I do something that would identify and change if the sbox_godmode is 1, or 0?

Here is the code I have so far.
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.duel( calling_ply, target_ply )
  4.         ply:ChatPrint( calling_ply "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  5.         hook.Add( "AcceptCheck", "AcceptDuel", function( target_ply )
  6.                 text = string.lower( text )
  7.                 if ( text == "!accept" ) then
  8.                 sbox_godmode = 0
  9.                 game.ConsoleCommand( "ulx god *" )
  10.                 game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply )
  11.                 function GM:PlayerDeath( victim, inflictor, attacker )
  12.                         if ( victim == attacker ) then
  13.                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  14.                                 game.ConsoleCommand( "ulx ungod *" )
  15.                                 sbox_godmode = 1
  16.                         else
  17.                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  18.                                 game.ConsoleCommand( "ulx ungod *" )
  19.                                 sbox_godmode = 1
  20.                         end
  21.                 end
  22.                 end
  23.         end )
  24. end
  25. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  26. duel:defaultAccess( ULib.ACCESS_ALL )
  27. duel:help( "Challenge a friend to a duel. !duel NAME." )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #1 on: October 30, 2015, 05:22:51 pm »
I'm not sure exactly what you want, so I'll just give you a general overview of if statements. If you can clarify your question I'd be happy to give an example more specific to your case.

If statements are in the form of

Code: [Select]
if condition then
  code to execute
end

For example:

Code: [Select]
if myVariable == 1 then
  myOtherVariable = 5
end
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #2 on: October 30, 2015, 07:22:25 pm »
What I am aiming to do is check to see if sbox_godmode (godmode enabler in SP and MP) is 1, meaning it's on. I forgot to add an else in the if statement if it's not sbox_godmode is 1. I added notes to give an overview of what I'm doing

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity" -- Everything needs a category.
  2.  
  3. function ulx.duel( calling_ply, target_ply ) -- Creating the command, calling_ply starts the duel, target_ply is the one needing to accept the duel
  4.         ply:ChatPrint( calling_ply "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply ) -- Tells the player that he can accept the duel. Below allows only the target_ply can accept. TO DO - Add a timer so the accept doesn't take forever. Is that easy to add in?
  5.         hook.Add( "AcceptCheck", "AcceptDuel", function( target_ply ) -- Starting a hook to detect the !accept. !accept is a chat trigger, not a ULX command. TO DO - Add a !deny that stops the timer and announces that you declined.
  6.                 text = string.lower( text ) -- Random, why caps lock bro?
  7.                 if ( text == "!accept" ) then -- Chat trigger !accept
  8.                 sbox_godmode = 0 -- I think this will change sbox_godmode. (The question!) or I can game.ConsoleCommand( "sbox_godmode 1" ) BUT I am curious if I can put it in the if statement.
  9.                 game.ConsoleCommand( "ulx god *" ) -- Why be able to kill everyone but the target?!
  10.                 game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Sorry target/calling_ply, you are duelling!
  11.                 function GM:PlayerDeath( victim, inflictor, attacker ) -- I found this online somewhere. I think this will announce the death.
  12.                         if ( victim == attacker ) then
  13.                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  14.                                 game.ConsoleCommand( "ulx ungod *" )
  15.                                 sbox_godmode = 1
  16.                         else
  17.                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  18.                                 game.ConsoleCommand( "ulx ungod *" ) -- Makes the god normal. Then enables sbox_godmode below.
  19.                                 sbox_godmode = 1
  20.                         end
  21.                 end
  22.                 end
  23.         end )
  24. end
  25. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" ) -- Declares its a ulx command, etc.
  26. duel:defaultAccess( ULib.ACCESS_ALL )
  27. duel:help( "Challenge a friend to a duel. !duel NAME." )
  28.  

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #3 on: October 30, 2015, 08:17:38 pm »
I don't see an if statement checking if sbox_godmode is 1 anywhere in that code. Anyways, since sbox_godmode is a console variable, you would use GetConVar( "sbox_godmode") to get the convar, and then ConVar:GetBool() to get it's value. For example:

Code: Lua
  1. sbox_godmode = GetConVar( "sbox_godmode" )
  2. if sbox_godmode:GetBool() then
  3.   --code to run if sbox_godmode is true (1)
  4. elseif not sbox_godemode:GetBool() then
  5.   --code to run if sbox_godemode is false (0)
  6. end
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Quick question on if statements
« Reply #4 on: October 30, 2015, 09:02:59 pm »
 
I don't see an if statement checking if sbox_godmode is 1 anywhere in that code. Anyways, since sbox_godmode is a console variable, you would use GetConVar( "sbox_godmode") to get the convar, and then ConVar:GetBool() to get it's value. For example:

Code: Lua
  1. sbox_godmode = GetConVar( "sbox_godmode" )
  2. if sbox_godmode:GetBool() then
  3.   --code to run if sbox_godmode is true (1)
  4. elseif not sbox_godemode:GetBool() then
  5.   --code to run if sbox_godemode is false (0)
  6. end

If I can nitpick for a moment, the elseif statement here is redundant.  A simple "else" would suffice, since you're checking for what is defined as a binary condition (sbox_godmode:GetBool() is either true or it isn't).

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #5 on: October 31, 2015, 06:13:31 am »
After adding it to my server it looks like it will work, I havent tried it on someone but heres the final code.

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.duel( calling_ply, target_ply )
  4.         ply:ChatPrint( calling_ply "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  5.         hook.Add( "AcceptCheck", "AcceptDuel", function( target_ply )
  6.                 text = string.lower( text )
  7.                 if ( text == "!accept" ) then
  8.                         sbox_godmode = GetConVar( "sbox_godmode" )
  9.                         if sbox_godmode:GetBool() then
  10.                                 game.ConsoleCommand( "sbox_godmode 0" ) -- Back up on 1/0. Does not print value, have to make sure either way
  11.                                 game.ConsoleCommand( "ulx god *" ) -- Other player protection
  12.                                 game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  13.                                 function GM:PlayerDeath( victim, inflictor, attacker )
  14.                                         if ( victim == attacker ) then -- Print death
  15.                                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  16.                                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  17.                                                 game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  18.                                                 game.ConsoleCommand( "sbox_godmode 1" )
  19.                                                 PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  20.                                                 if ply:IsUserGroup( "superadmin" ) then
  21.                                                         hook.Add( "SGodmode", "SwitchGod", function( command )
  22.                                                                 text = string.lower( text )
  23.                                                                 if ( text == "!sgodmode" ) then
  24.                                                                         game.ConsoleCommand( "sbox_godmode 0" )
  25.                                                                 end
  26.                                                         end )
  27.                                                 end                            
  28.                                         elseif not sbox_godemode:GetBool() then
  29.                                                 game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  30.                                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  31.                                                 PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  32.                                                 game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  33.                                                 game.ConsoleCommand( "sbox_godmode 1" )
  34.                                                 PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  35.                                                 if ply:IsUserGroup( "superadmin" ) then
  36.                                                         hook.Add( "SGodmode", "SwitchGod", function( command )
  37.                                                                 text = string.lower( text )
  38.                                                                 if ( text == "!sgodmode" ) then
  39.                                                                         game.ConsoleCommand( "sbox_godmode 0" )
  40.                                                                 end
  41.                                                         end )
  42.                                                 end
  43.                                         end
  44.                                 end
  45.                         end
  46.                 end
  47.         end )
  48. end
  49. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  50. duel:defaultAccess( ULib.ACCESS_ALL )
  51. duel:help( "Challenge a friend to a duel. !duel NAME." )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #6 on: October 31, 2015, 08:34:43 am »

If I can nitpick for a moment, the elseif statement here is redundant.  A simple "else" would suffice, since you're checking for what is defined as a binary condition (sbox_godmode:GetBool() is either true or it isn't).

Yeah, that's true. I guess I wasn't thinking.

After adding it to my server it looks like it will work, I havent tried it on someone but heres the final code.

I'm not sure what you want, but I'll just give you a few general comments. Your code is very garbled. For starters, it isn't a good idea to use unnamed function declaration for a 40 line function. You can move your entire "AcceptDuel" function outside of the ulx.duel function, and then just reference it in the hook.Add.

Code: Lua
  1. local function acceptDuel( ply )
  2.   --code
  3. end
  4.  
  5. function ulx.duel( calling_ply, target_ply )
  6.   hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  7. end

On line 1 of the "AcceptDuel" function you are referencing 'text', a variable not declared in your code. Unless this is defined somewhere else in your file and you aren't showing us it, this will throw an error.

Next, on line 2 you have an if statement that contains the rest of the function. To make it easier to read, you can just make an if statement to check for the opposite condition and return if it is true.

Code: Lua
  1. if not text == "!accept" then return end

This removes an entire layer of indentation from your code, and removes one of the eight end statements you have at the end.

Again, on line 4 you have another if statement that contains the rest of the function. You can use the previously mentioned technique of simply returning if it is false, or you can combine it into the previous if statement like so:

Code: Lua
  1. if not text == "!accept" or not sbox_godmode:GetBool() then return end

For this to work you would have to move the declaration statement of sbox_godmode above the if statement.

Then on line 8 you are declaring a function inside a function. This is messy, and introduces unnecessary layers of indentation. Instead, you can declare a function outside and then do this:

Code: Lua
  1. function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )

Of course, you would need to change playerDeath to whatever you named the outside function.

Now in the playerDeath function, on lines 9 and 10 you reference the variable 'text' which is still not declared.

For some reason in this function you have an if statement like this:

Code: Lua
  1. if ( victim == attacker ) then
  2.   --code
  3. elseif not sbox_godemode:GetBool() then
  4.   --code
  5. end

I'm not sure if this was intentional, or if you meant to have the elseif to continue from your initial if loop (the one that surrounded all of the code). If it was intentional, then you need to declare sbox_godmode (I assume godemode was an accidental spelling error) again (because we've moved it to it's own function). If you want, you could just declare sbox_godmode outside of any functions thus allowing both functions to access it.

In this if statement you also reference text, which is still not declared.

After all of these changes, I got this:

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. sbox_godmode = GetConVar( "sbox_godmode" )
  4.  
  5. function playerDeath( victim, inflictor, attacker )
  6.   if ( victim == attacker ) then -- Print death
  7.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  8.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  9.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  10.     game.ConsoleCommand( "sbox_godmode 1" )
  11.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  12.     if ply:IsUserGroup( "superadmin" ) then
  13.       hook.Add( "SGodmode", "SwitchGod", function( command )
  14.         text = string.lower( text ) --text isn't defined!
  15.         if ( text == "!sgodmode" ) then --still not defined
  16.           game.ConsoleCommand( "sbox_godmode 0" )
  17.         end
  18.       end )
  19.     end
  20.   elseif not sbox_godemode:GetBool() then
  21.     game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  22.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  23.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  24.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  25.     game.ConsoleCommand( "sbox_godmode 1" )
  26.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  27.     if ply:IsUserGroup( "superadmin" ) then
  28.       hook.Add( "SGodmode", "SwitchGod", function( command )
  29.         text = string.lower( text ) --text isn't defined!
  30.         if ( text == "!sgodmode" ) then --still not defined
  31.           game.ConsoleCommand( "sbox_godmode 0" )
  32.         end
  33.       end )
  34.     end
  35.   end
  36. end
  37.  
  38. local function acceptDuel( ply )
  39.   text = string.lower( text ) --text isn't defined!
  40.   if not text == "!accept" or not sbox_godmode:GetBool() then return end --still not defined
  41.   game.ConsoleCommand( "sbox_godmode 0" ) -- Back up on 1/0. Does not print value, have to make sure either way
  42.   game.ConsoleCommand( "ulx god *" ) -- Other player protection
  43.   game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  44.   function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )
  45. end
  46.  
  47. function ulx.duel( calling_ply, target_ply )
  48.         ply:ChatPrint( calling_ply "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  49.         hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  50. end
  51. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  52. duel:defaultAccess( ULib.ACCESS_ALL )
  53. duel:help( "Challenge a friend to a duel. !duel NAME." )

The text value isn't declared, so that will give you errors but other than that I don't think there are any actual lua errors. Whether or not the code actually does what you want, that is another story.
« Last Edit: October 31, 2015, 09:09:32 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #7 on: October 31, 2015, 06:33:18 pm »
After reviewing that its probably much better than a way errored version of newer code I made so thanks! I myself did notice that I missed a comma as-well, probably resulting in another error. I redefined "text" in the functions as its what the player is printing in chat (I mainly took it from wiki.garrysmod.com). :3 You help me a lot <3

I fixed the comma. ::)

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. sbox_godmode = GetConVar( "sbox_godmode" )
  4.  
  5. function playerDeath( victim, inflictor, attacker )
  6.   if ( victim == attacker ) then -- Print death
  7.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  8.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  9.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  10.     game.ConsoleCommand( "sbox_godmode 1" )
  11.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  12.     if ply:IsUserGroup( "superadmin" ) then
  13.       hook.Add( "SGodmode", "SwitchGod", function( text, command )
  14.         text = string.lower( text )
  15.         if ( text == "!sgodmode" ) then
  16.           game.ConsoleCommand( "sbox_godmode 0" )
  17.         end
  18.       end )
  19.     end
  20.   elseif not sbox_godemode:GetBool() then
  21.     game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  22.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  23.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  24.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  25.     game.ConsoleCommand( "sbox_godmode 1" )
  26.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  27.     if ply:IsUserGroup( "superadmin" ) then
  28.       hook.Add( "SGodmode", "SwitchGod", function( command )
  29.         text = string.lower( text ) --text isn't defined!
  30.         if ( text == "!sgodmode" ) then --still not defined
  31.           game.ConsoleCommand( "sbox_godmode 0" )
  32.         end
  33.       end )
  34.     end
  35.   end
  36. end
  37.  
  38. local function acceptDuel( ply, text )
  39.   text = string.lower( text ) --text isn't defined!
  40.   if not text == "!accept" or not sbox_godmode:GetBool() then return end
  41.   game.ConsoleCommand( "sbox_godmode 0" )
  42.   game.ConsoleCommand( "ulx god *" ) -- Other player protection
  43.   game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  44.   function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )
  45. end
  46.  
  47. function ulx.duel( calling_ply, target_ply )
  48.         ply:ChatPrint( calling_ply, "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  49.         hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  50. end
  51. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  52. duel:defaultAccess( ULib.ACCESS_ALL )
  53. duel:help( "Challenge a friend to a duel. !duel NAME." )
  54.  

P.S. I can spell godmode "godemode", and I think I added in text properly? *facepalm*
« Last Edit: October 31, 2015, 06:35:06 pm by WispySkies »

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #8 on: October 31, 2015, 06:48:35 pm »
Great .-.
Lua Error: [ERROR] addons/ulx/lua/ulx/modules/sh/duel.lua:44: '(' expected near '=' 1. unknown - addons/ulx/lua/ulx/modules/sh/duel.lua:0

:o
Does it have to be ==?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #9 on: October 31, 2015, 10:11:21 pm »
Being able to see your code would be nice :-)

Even we mighty and powerful lua developers can't read the files on your computer telepathically :p
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #10 on: November 01, 2015, 04:09:28 am »
Being able to see your code would be nice :-)

Even we mighty and powerful lua developers can't read the files on your computer telepathically :p
Whoops sorry about that ;)
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. sbox_godmode = GetConVar( "sbox_godmode" )
  4.  
  5. function playerDeath( victim, inflictor, attacker )
  6.   if ( victim == attacker ) then -- Print death
  7.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  8.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  9.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  10.     game.ConsoleCommand( "sbox_godmode 1" )
  11.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  12.     if ply:IsUserGroup( "superadmin" ) then
  13.       hook.Add( "SGodmode", "SwitchGod", function( text, command )
  14.         text = string.lower( text )
  15.         if ( text == "!sgodmode" ) then
  16.           game.ConsoleCommand( "sbox_godmode 0" )
  17.         end
  18.       end )
  19.     end
  20.   elseif not sbox_godemode:GetBool() then
  21.     game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  22.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  23.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  24.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  25.     game.ConsoleCommand( "sbox_godmode 1" )
  26.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  27.     if ply:IsUserGroup( "superadmin" ) then
  28.       hook.Add( "SGodmode", "SwitchGod", function( command )
  29.         text = string.lower( text ) --text isn't defined!
  30.         if ( text == "!sgodmode" ) then --still not defined
  31.           game.ConsoleCommand( "sbox_godmode 0" )
  32.         end
  33.       end )
  34.     end
  35.   end
  36. end
  37.  
  38. local function acceptDuel( ply, text )
  39.   text = string.lower( text )
  40.   if not text == "!accept" or not sbox_godmode:GetBool() then return end
  41.   game.ConsoleCommand( "sbox_godmode 0" )
  42.   game.ConsoleCommand( "ulx god *" ) -- Other player protection
  43.   game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  44.   function GM:PlayerDeath = playerDeath( victim, inflictor, attacker )
  45. end
  46.  
  47. function ulx.duel( calling_ply, target_ply )
  48.         ply:ChatPrint( calling_ply, "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  49.         hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  50. end
  51. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  52. duel:defaultAccess( ULib.ACCESS_ALL )
  53. duel:help( "Challenge a friend to a duel. !duel NAME." )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #11 on: November 01, 2015, 08:26:59 am »
I don't think you need/should have the 'function' keyword there at all, because playerDeath is already a function. If you put the function keyword, the lua interpreter expects a function declaration. It should be:

Code: Lua
  1. GM:PLayerDeath = playerDeath()
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Quick question on if statements
« Reply #12 on: November 01, 2015, 02:11:27 pm »
I don't think you need/should have the 'function' keyword there at all, because playerDeath is already a function. If you put the function keyword, the lua interpreter expects a function declaration. It should be:

Code: Lua
  1. GM:PLayerDeath = playerDeath()
You don't need the function call either.
Just do this:
Code: Lua
  1. GM:PlayerDeath = playerDeath
Or, the better way
Code: Lua
  1. hook.Add("PlayerDeath", "DuelPlayerDeathHandler", playerDeath)
bw81@ulysses-forums ~ % whoami
Homepage

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #13 on: November 01, 2015, 02:59:47 pm »
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. sbox_godmode = GetConVar( "sbox_godmode" )
  4.  
  5. function playerDeath( victim, inflictor, attacker )
  6.   if ( victim == attacker ) then -- Print death
  7.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  8.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  9.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  10.     game.ConsoleCommand( "sbox_godmode 1" )
  11.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  12.     if ply:IsUserGroup( "superadmin" ) then
  13.       hook.Add( "SGodmode", "SwitchGod", function( text, command )
  14.         text = string.lower( text )
  15.         if ( text == "!sgodmode" ) then
  16.           game.ConsoleCommand( "sbox_godmode 0" )
  17.         end
  18.       end )
  19.     end
  20.   elseif not sbox_godemode:GetBool() then
  21.     game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  22.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  23.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  24.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  25.     game.ConsoleCommand( "sbox_godmode 1" )
  26.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  27.     if ply:IsUserGroup( "superadmin" ) then
  28.       hook.Add( "SGodmode", "SwitchGod", function( command )
  29.         text = string.lower( text ) --text isn't defined!
  30.         if ( text == "!sgodmode" ) then --still not defined
  31.           game.ConsoleCommand( "sbox_godmode 0" )
  32.         end
  33.       end )
  34.     end
  35.   end
  36. end
  37.  
  38. local function acceptDuel( ply, text )
  39.   text = string.lower( text )
  40.   if not text == "!accept" or not sbox_godmode:GetBool() then return end
  41.   game.ConsoleCommand( "sbox_godmode 0" )
  42.   game.ConsoleCommand( "ulx god *" ) -- Other player protection
  43.   game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  44.   hook.Add("PlayerDeath", "DuelPlayerDeathHandler", playerDeath)
  45. end
  46.  
  47. function ulx.duel( calling_ply, target_ply )
  48.         ply:ChatPrint( calling_ply, "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  49.         hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  50. end
  51. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  52. duel:defaultAccess( ULib.ACCESS_ALL )
  53. duel:help( "Challenge a friend to a duel. !duel NAME." )

Testing this now ::)

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #14 on: November 01, 2015, 04:15:28 pm »
I noticed in menu there is no ply to duel so I tweaked it. Is this alright?
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. sbox_godmode = GetConVar( "sbox_godmode" )
  4.  
  5. function playerDeath( victim, inflictor, attacker )
  6.   if ( victim == attacker ) then -- Print death
  7.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." )
  8.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  9.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  10.     game.ConsoleCommand( "sbox_godmode 1" )
  11.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  12.     if ply:IsUserGroup( "superadmin" ) then
  13.       hook.Add( "SGodmode", "SwitchGod", function( text, command )
  14.         text = string.lower( text )
  15.         if ( text == "!sgodmode" ) then
  16.           game.ConsoleCommand( "sbox_godmode 0" )
  17.         end
  18.       end )
  19.     end
  20.   elseif not sbox_godemode:GetBool() then
  21.     game.ConsoleCommand( "sbox_godmode 0" ) -- Line 10.
  22.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " committed suicide." ) -- Print death
  23.     PrintMessage( HUD_PRINTTALK, victim:Name() .. " was killed by " .. attacker:Name() .. "." )
  24.     game.ConsoleCommand( "ulx ungod *" ) --Remove all ULX gods
  25.     game.ConsoleCommand( "sbox_godmode 1" )
  26.     PrintMessage( HUD_PRINTTALK, "Finished duel! Admin, type !sgodmode to switch the sbox_godmode to 0." )
  27.     if ply:IsUserGroup( "superadmin" ) then
  28.       hook.Add( "SGodmode", "SwitchGod", function( command )
  29.         text = string.lower( text ) --text isn't defined!
  30.         if ( text == "!sgodmode" ) then --still not defined
  31.           game.ConsoleCommand( "sbox_godmode 0" )
  32.         end
  33.       end )
  34.     end
  35.   end
  36. end
  37.  
  38. local function acceptDuel( ply, text )
  39.   text = string.lower( text )
  40.   if not text == "!accept" or not sbox_godmode:GetBool() then return end
  41.   game.ConsoleCommand( "sbox_godmode 0" )
  42.   game.ConsoleCommand( "ulx god *" ) -- Other player protection
  43.   game.ConsoleCommand( calling_ply, "ulx ungod #A,#T", target_ply ) -- Ungod the players so they can fight
  44.   hook.Add("PlayerDeath", "DuelPlayerDeathHandler", playerDeath)
  45. end
  46.  
  47. function ulx.duel( ply, target_ply )
  48.         ply:ChatPrint( ply, "#A has challenged #T to a duel! Type !accept to confirm the duel!", target_ply )
  49.         hook.Add( "AcceptCheck", "AcceptDuel", acceptDuel( target_ply ) )
  50. end
  51. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel,"!duel" )
  52. duel:defaultAccess( ULib.ACCESS_ALL )
  53. duel:help( "Challenge a friend to a duel. !duel NAME." )

  • Print