• 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.
Re: Quick question on if statements
« Reply #15 on: November 02, 2015, 04:49:03 am »
Since this has still proven not to work for me I'm going to redo the command something like this.
Code: Lua
  1. -- This is written directly on the forums with an iPhone without tab, if I make errors that are easily done on a phone without an editor just shhh ;)
  2.  
  3. local CATEGORY_NAME = "Advaced Simplicity"
  4.  
  5. ulx_game_table = {} -- Add game types, FFA, DM, etc.
  6. function ulx.duel( calling_ply, game, weapon, target )
  7. target:ChatPrint( calling_ply, "#A wants to challenge you to a duel with a #s. Do you want to accept?" )
  8. calling_ply:ChatPrint( target, "You have challenged #T to a duel." )
  9. hook.Add( "Accept", "AcceptDuel", function( target, text )
  10. text = string.lower( text )
  11. if ( text == "!accept" ) then
  12. calling_ply:ChatPrint( "Choose your weapon. You must know the name, such as weapon_357." )
  13. hook.Add( "ChooseGun", "GunChoice", function( calling_ply, weapon )
  14. weapon = string.lower( weapon )
  15. calling_ply:Give( weapon )
  16. target:Give( weapon )
  17. end ) -- End ChooseGun hook
  18. -- Insert all godmode, ULX God, chat prints here.
  19. end ) -- End 1st hook
  20. if not calling_ply:IsAlive() or not target:IsAlive() then
  21. PrintMessage( HUD_PRINTTALK, "The duel has ended." )
  22. -- Re god stuff here
  23. end
  24. end
  25. -- I'm busy now, ran out of time to add the local duel = ulx.command stuff. I'll edit this post when I get the chance.
« Last Edit: November 02, 2015, 04:50:55 am by WispySkies »

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #16 on: November 02, 2015, 08:06:19 am »
It's much better if your test your code and then come to us if you have any errors. We're not your personal bug-finding squad.
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 #17 on: November 02, 2015, 12:29:33 pm »
It's much better if your test your code and then come to us if you have any errors. We're not your personal bug-finding squad.
I don't see how you came to that conclusion at all, since it's still filled with errors and I'm changing it I'm letting you guys know. When did I even say you were a squad?

Offline monkeymacman

  • Newbie
  • *
  • Posts: 44
  • Karma: 13
Re: Quick question on if statements
« Reply #18 on: November 02, 2015, 12:56:34 pm »
You forgot to add in a player argument in the command (I think, I'm on mobile right now)  so you should have been alright with the calling_ply instead of replacing it with ply, as far as I can see all you did was change a variable. You'd just need to add the player arg under the acces
duel:addParam{ type=ULib.cmds.PlayerArg }

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #19 on: November 02, 2015, 12:59:52 pm »
Oh, I see that .-. Im still going with the new one as my friend suggested different types and not allowing the spawn menu.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #20 on: November 02, 2015, 01:41:20 pm »
Okay so I re did the command, I just need help on one thing. Checking if a player says !accept, in the hook it adds a function with ply and text, if the text is !accept then do this, the reason why it never worked because its a new function WITHOUT the arguments from the ulx.duel function so I need help working around that. This is what I have so far.

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.duel( ply, weapon, target )
  4.         ply:ChatPrint( target, "Challenged #T to a duel with #s.", weapon )
  5.         target:ChatPrint( ply, "#A challenged you to a duel with #s.", weapon )
  6. -- Insert detection of how the target says !accept, the function inside the hook only added more arguments meaning that ply was never taken into account thus the errors and it not working.
  7.         sbox_godmode:GetConVar( "sbox_godmode" ) -- Gets original value of sbox_godmode, see after player death.
  8.         game.ConsoleCommand( "sbox_godmode 0" )
  9.         game.ConsoleCommand( "ulx god *" )
  10.         game.ConsoleCommand( ply, "ulx ungod #A,#T", target )
  11.         local function DisallowSpawnMenu( )
  12.                 if not ply:IsUserGroup( "superadmin" ) or target:IsUserGroup( "superadmin" ) then
  13.                         return false
  14.                 end
  15.         end
  16.  
  17.         hook.Add( "SpawnMenuOpen", "DisallowSpawnMenu", DisallowSpawnMenu)
  18.         ply:Give( weapon )
  19.         target:Give( weapon )
  20.         if weapon == "m9k_davy_crockett" or weapon == "m9k_orbital_strike" then
  21.                 ply:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  22.                 target:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  23.         end
  24.         PrintMessage( HUD_PRINTTALK, "Fight!" )
  25.         if not ply:IsAlive() or target:IsAlive() then
  26.                 PrintMessage( HUD_PRINTTALK, "Duel over!" )
  27.                 local function DisallowSpawnMenu( ) -- Re-allow spawn menu
  28.                         return true
  29.                 end
  30.  
  31.                 hook.Add( "SpawnMenuOpen2", "DisallowSpawnMenu2", DisallowSpawnMenu)
  32.                 game.ConsoleCommand( "ulx ungod *" )
  33.                 game.ConsoleCommand( "sbox_godmode" .. sbox_godmode:GetBool( "sbox_godmode" ) ) -- Checks the original value to set it back to the normal sbox_godmode.
  34.         end
  35. end
  36. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel, "!duel" )
  37. duel:defaultAccess( ULib.ACCESS_ALL )
  38. duel:addParam{ type = ULib.cmds.PlayerArg }
  39. duel:help( "Duel a friend or enemy!" )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #21 on: November 02, 2015, 02:44:17 pm »
I'm not exactly sure if this is your problem, but it sounds like you want to pass the ply argument from the ulx.duel function to a new function? Just make a new function that has an argument player, and pass it.

Code: Lua
  1. function myFunction( player )
  2.   --do something
  3. end
  4.  
  5. function ulx.duel( ply, weapon, target )
  6.   --do some other stuff
  7.   myFunction
  8.   --do more stuff( ply )
  9. 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 #22 on: November 02, 2015, 03:37:25 pm »
I'm not exactly sure if this is your problem, but it sounds like you want to pass the ply argument from the ulx.duel function to a new function? Just make a new function that has an argument player, and pass it.

Code: Lua
  1. function myFunction( player )
  2.   --do something
  3. end
  4.  
  5. function ulx.duel( ply, weapon, target )
  6.   --do some other stuff
  7.   myFunction
  8.   --do more stuff( ply )
  9. end
Actually the opposite I think. In the past code, the calling_ply and target_ply would never run because all the commands were stored in the function inside the hook meaning it doesnt call the previous ulx.duel function. I added notes into the code below to give you a better explanation of what I mean. Hope it helps ::) Essentially a dummy would understand what I did.

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity" -- Category name
  2.  
  3. function ulx.duel( ply, weapon, target ) -- Make the command
  4.         ply:ChatPrint( target, "Challenged #T to a duel with #s.", weapon ) -- Re tell the calling_ply
  5.         target:ChatPrint( ply, "#A challenged you to a duel with #s.", weapon ) -- Alerts the target
  6. --[[
  7.  
  8. A hook! The messed up thing that ruined the last code as it did not incorperate the previous function arguments, so all commands were in the function( ply, text ) crap, which does not use the ulx.duel function with a ( ply, weapon, and a target ). So what I need here is something that detects if the target types !accept and if so run the commands below, not the ones inside the hook function.
  9.  
  10. ]]--
  11.         sbox_godmode:GetConVar( "sbox_godmode" ) -- Gets original value of sbox_godmode, used later.
  12.         game.ConsoleCommand( "sbox_godmode 0" ) -- Makes the godmode off for all
  13.         game.ConsoleCommand( "ulx god *" ) -- Gods all the affected players from the sbox_godmode 1 revomal.
  14.         game.ConsoleCommand( ply, "ulx ungod #A,#T", target ) -- Un gods the ULX god from the Player and the Target
  15.         local function DisallowSpawnMenu( ) -- Blocks spawn menu to avoid cheating with other guns
  16.                 if not ply:IsUserGroup( "superadmin" ) or target:IsUserGroup( "superadmin" ) then -- I want admins to be able to use the menu if things get out of hand.
  17.                         return false -- So if they are an admin, return = end script, dont block the menu
  18.                 end -- End the if
  19.         end -- end the DisallowSpawnMenu function
  20.  
  21.         hook.Add( "SpawnMenuOpen", "DisallowSpawnMenu", DisallowSpawnMenu) -- A hook for the disallowing of the menu
  22.         ply:Give( weapon ) -- Gives the player the specified weapon
  23.         target:Give( weapon ) -- Gives the player the specified weapon
  24.         if weapon == "m9k_davy_crockett" or weapon == "m9k_orbital_strike" then -- I have the Addon M9K Specialties and the Davy unwelds/launches EVERY thing, orbitals, a pain in the butt because they are an insta kill.
  25.                 ply:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" ) -- URS would not block the ply:Give( weapon ), this blocks that.
  26.                 target:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" ) -- URS would not block the ply:Give( weapon ), this blocks that.
  27.         end -- End the if about Davies
  28.         PrintMessage( HUD_PRINTTALK, "Fight!" ) -- Alert to fight
  29.         if not ply:IsAlive() or target:IsAlive() then -- If they are dead, game over.
  30.                 PrintMessage( HUD_PRINTTALK, "Duel over!" ) -- Say its over.
  31.                 local function DisallowSpawnMenu( ) -- Re-allow spawn menu
  32.                         return true -- No admin necessary here, just to end the function but turn on the Menu.
  33.                 end -- End the function
  34.  
  35.                 hook.Add( "SpawnMenuOpen2", "DisallowSpawnMenu2", DisallowSpawnMenu) -- Same hook, renamed, as we had another one
  36.                 game.ConsoleCommand( "ulx ungod *" ) -- Re god everyone
  37.                 game.ConsoleCommand( "sbox_godmode" .. sbox_godmode:GetBool( "sbox_godmode" ) ) -- Grabs the value of the sbox_godmode before the game and sets it to that
  38.         end -- End the dead if statement
  39. end -- End the duel function
  40. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel, "!duel" ) -- Says its a ulx command, with the associated category, and the command to use it
  41. duel:defaultAccess( ULib.ACCESS_ALL ) -- Everyone should be able to use the command
  42. duel:addParam{ type = ULib.cmds.PlayerArg } -- Monkeymacman said this is necessary?
  43. duel:help( "Duel a friend or enemy!" ) -- Help for the command.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #23 on: November 02, 2015, 06:51:13 pm »
I guess I'm not "essentially a dummy" because I don't understand what you did nor what you want. I guess I'm just being slow.

Never mind, I guess I truly was being slow. I re-read your comment and I think I understand what you want now. You want to check if someone types !accept and if so run some code.

To find out if a player typed something, you use the hook "PlayerSay".

Code: Lua
  1. hook.Add( "PlayerSay", "PlayerSayExample", myFunction( ply, text, team ) )

You only want to check the chat of the target, so make an if statement that returns if the ply argument isn't equal to the target.

You only want to do something if the text typed was "!accept", so make another if statement that returns if the text argument isn't equal to that or just incorporate it into your previous if statement.

It doesn't matter if it's in team chat or not, so you can just ignore the last argument.

All put together, it should look something like this:

Code: Lua
  1. hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
  2.   if not ply == target or not text == "!accept" then return true end
  3.   --run code here
  4. end )
« Last Edit: November 02, 2015, 07:10:58 pm 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 #24 on: November 02, 2015, 07:17:23 pm »
I guess I'm not "essentially a dummy" because I don't understand what you did nor what you want. I guess I'm just being slow.

Never mind, I guess I truly was being slow. I re-read your comment and I think I understand what you want now. You want to check if someone types !accept and if so run some code.

To find out if a player typed something, you use the hook "PlayerSay".

Code: Lua
  1. hook.Add( "PlayerSay", "PlayerSayExample", myFunction( ply, text, team ) )

You only want to check the chat of the target, so make an if statement that returns if the ply argument isn't equal to the target.

You only want to do something if the text typed was "!accept", so make another if statement that returns if the text argument isn't equal to that or just incorporate it into your previous if statement.

It doesn't matter if it's in team chat or not, so you can just ignore the last argument.

All put together, it should look something like this:

Code: Lua
  1. hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
  2.   if not ply == target or not text == "!accept" then return true end
  3.   --run code here
  4. end )

By "essentially a dummy" could get is was because of all the notes I added in. Even if I added that in, how would it work? From what I guess its like this, I insert that hook before the code, call the function after the duel has started to see if a player says !accept, and then it continues the code in the function. But still, the trickiest part Im still trying to explain to you and even myself is in the hook, it creates the myFunction with a (ply, text, team ), but since its different (myFunction, not the duel function with the ( TARGET ), I see it as checking if anyone says !accept, not the target, making this all a giant mess of how this links to what. So do you get what I mean? Or do I not understand this and I should just put it in here.

Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. hook.Add( "PlayerSay", "PlayerSayExample", myFunction( ply, text, team ) )
  4. function ulx.duel( ply, weapon, target )
  5.         ply:ChatPrint( target, "Challenged #T to a duel with #s.", weapon )
  6.         target:ChatPrint( ply, "#A challenged you to a duel with #s.", weapon )
  7.         myFunction
  8.         sbox_godmode:GetConVar( "sbox_godmode" ) -- Gets original value of sbox_godmode, see after player death.
  9.         game.ConsoleCommand( "sbox_godmode 0" )
  10.         game.ConsoleCommand( "ulx god *" )
  11.         game.ConsoleCommand( ply, "ulx ungod #A,#T", target )
  12.         local function DisallowSpawnMenu( )
  13.                 if not ply:IsUserGroup( "superadmin" ) or target:IsUserGroup( "superadmin" ) then
  14.                         return false
  15.                 end
  16.         end
  17.  
  18.         hook.Add( "SpawnMenuOpen", "DisallowSpawnMenu", DisallowSpawnMenu)
  19.         ply:Give( weapon )
  20.         target:Give( weapon )
  21.         if weapon == "m9k_davy_crockett" or weapon == "m9k_orbital_strike" then
  22.                 ply:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  23.                 target:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  24.         end
  25.         PrintMessage( HUD_PRINTTALK, "Fight!" )
  26.         if not ply:IsAlive() or target:IsAlive() then
  27.                 PrintMessage( HUD_PRINTTALK, "Duel over!" )
  28.                 local function DisallowSpawnMenu( ) -- Re-allow spawn menu
  29.                         return true
  30.                 end
  31.  
  32.                 hook.Add( "SpawnMenuOpen2", "DisallowSpawnMenu2", DisallowSpawnMenu)
  33.                 game.ConsoleCommand( "ulx ungod *" )
  34.                 game.ConsoleCommand( "sbox_godmode" .. sbox_godmode:GetBool( "sbox_godmode" ) ) -- Checks the original value to set it back to the normal sbox_godmode.
  35.         end
  36. end
  37. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel, "!duel" )
  38. duel:defaultAccess( ULib.ACCESS_ALL )
  39. duel:addParam{ type = ULib.cmds.PlayerArg }
  40. duel:help( "Duel a friend or enemy!" )
  41.  

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #25 on: November 02, 2015, 07:19:33 pm »
Oh god I am blind. Extremely blind! I just re read what I posted and you posted. *facepalm* Ill add it in, and then give you an update.

Offline WispySkies

  • Full Member
  • ***
  • Posts: 144
  • Karma: 0
  • I make random commands and Lua errors.
Re: Quick question on if statements
« Reply #26 on: November 02, 2015, 07:25:08 pm »
Okay so I tweaked around with it, Im not sure if this is what you meant, but it looks alright.
Code: Lua
  1. local CATEGORY_NAME = "Advanced Simplicity"
  2.  
  3. function ulx.duel( ply, weapon, target )
  4.         ply:ChatPrint( target, "Challenged #T to a duel with #s.", weapon )
  5.         target:ChatPrint( ply, "#A challenged you to a duel with #s.", weapon )
  6.         hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
  7.                 if not ply == target or not text == "!accept" then return true end
  8.                         sbox_godmode:GetConVar( "sbox_godmode" ) -- Gets original value of sbox_godmode, see after player death.
  9.                         game.ConsoleCommand( "sbox_godmode 0" )
  10.                         game.ConsoleCommand( "ulx god *" )
  11.                         game.ConsoleCommand( ply, "ulx ungod #A,#T", target )
  12.                         local function DisallowSpawnMenu( )
  13.                                 if not ply:IsUserGroup( "superadmin" ) or target:IsUserGroup( "superadmin" ) then
  14.                                         return false
  15.                                 end
  16.                         end
  17.  
  18.                         hook.Add( "SpawnMenuOpen", "DisallowSpawnMenu", DisallowSpawnMenu)
  19.                         ply:Give( weapon )
  20.                         target:Give( weapon )
  21.                         if weapon == "m9k_davy_crockett" or weapon == "m9k_orbital_strike" then
  22.                                 ply:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  23.                                 target:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
  24.                         end
  25.                         PrintMessage( HUD_PRINTTALK, "Fight!" )
  26.                         if not ply:IsAlive() or target:IsAlive() then
  27.                                 PrintMessage( HUD_PRINTTALK, "Duel over!" )
  28.                                 local function DisallowSpawnMenu( ) -- Re-allow spawn menu
  29.                                         return true
  30.                                 end
  31.  
  32.                                 hook.Add( "SpawnMenuOpen2", "DisallowSpawnMenu2", DisallowSpawnMenu)
  33.                                 game.ConsoleCommand( "ulx ungod *" )
  34.                                 game.ConsoleCommand( "sbox_godmode" .. sbox_godmode:GetBool( "sbox_godmode" ) ) -- Checks the original value to set it back to the normal sbox_godmode.
  35.                         end
  36.                 end
  37.         end )
  38. end
  39. local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel, "!duel" )
  40. duel:defaultAccess( ULib.ACCESS_ALL )
  41. duel:addParam{ type = ULib.cmds.PlayerArg }
  42. duel:help( "Duel a friend or enemy!" )
  43.  

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #27 on: November 03, 2015, 08:07:47 am »
The hook and the ply and text checking look fine. You still have a few issues with indentation, and there are a lot of nested functions but that's just personal preference.
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 #28 on: November 03, 2015, 11:07:09 am »
This is it with the indents fixed, although its spaces, Ill edit this post when I fix them. (Im on a school computer, gg, not my editor at home.)

Code: [Select]
local CATEGORY_NAME = "Advanced Simplicity"
 
function ulx.duel( ply, weapon, target )
    ply:ChatPrint( target, "Challenged #T to a duel with #s.", weapon )
    target:ChatPrint( ply, "#A challenged you to a duel with #s.", weapon )
    hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
        if not ply == target or not text == "!accept" then return true end
    sbox_godmode:GetConVar( "sbox_godmode" ) -- Gets original value of sbox_godmode, see after player death.
    game.ConsoleCommand( "sbox_godmode 0" )
    game.ConsoleCommand( "ulx god *" )
    game.ConsoleCommand( ply, "ulx ungod #A,#T", target )
    local function GM:SpawnMenuEnabled( )
        if ply:IsUserGroup( "superadmin" ) or target:IsUserGroup( "superadmin" ) then
            return true
        else
            return false
        end
    end
    ply:Give( weapon )
    target:Give( weapon )
    if weapon == "m9k_davy_crockett" or weapon == "m9k_orbital_strike" then
        ply:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
        target:ChatPrint( "Nice try getting past URS, Davy Crockets and Orbital Air Strikes are NOT allowed!" )
    end
    PrintMessage( HUD_PRINTTALK, "Fight!" )
    if not ply:IsAlive() or target:IsAlive() then
        PrintMessage( HUD_PRINTTALK, "Duel over!" )
        game.ConsoleCommand( "ulx ungod *" )
        game.ConsoleCommand( "sbox_godmode" .. sbox_godmode:GetBool( "sbox_godmode" ) ) -- Checks the original value to set it back to the normal sbox_godmode.
    end
    end )
end
local duel = ulx.command( CATEGORY_NAME, "ulx duel", ulx.duel, "!duel" )
duel:defaultAccess( ULib.ACCESS_ALL )
duel:addParam{ type = ULib.cmds.PlayerArg }
duel:help( "Duel a friend or enemy!" )

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Quick question on if statements
« Reply #29 on: November 03, 2015, 12:02:14 pm »
spaces > tabs

lets start a war!  :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.

  • Print