• Print

Author Topic: Need Help Finding and Comparing Weapons That A Player Is Holding  (Read 8359 times)

0 Members and 1 Guest are viewing this topic.

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Need Help Finding and Comparing Weapons That A Player Is Holding
« on: November 17, 2017, 02:28:44 pm »
So, I am working on a spawn protect addon and I am trying to make it so that when the player pulls out a weapon the spawn protect will go away. I would like the spawn protect to stay on when they spawn a weapon on the ground. I just want it to go off when they pull anything out other than the physics gun, tool gun, and camera. I can't figure out a way to compare the wepon that a player is holding to the weapons that I want to not be considered a weapon. I am new to LUA and this is all that I have come up with for this section of it. Any help would be  :)

Code: Lua
  1. local function GetWeapon()
  2.         local ply = LocalPlayer()
  3.         local wep = ply:GetActiveWeapon()
  4.         if(!wep.IsValid()) then return false end
  5.         if wep ~= weapon_physgun then
  6.                 SP = false end
  7.         if wep ~= gmod_tool then
  8.                 SP = false end
  9.         if wep ~= gmod_camera then
  10.                 SP = false end
  11.         print(wep)
  12. end
  13.  

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #1 on: November 17, 2017, 03:06:44 pm »
I also tried this but every time it would say that the OW(Old Weapon) it is not equal to weapon_physgun, gmod_tool, or gmod_camera and it would run the code in the if statement no matter what. Maybe I am comparing the OW to the weapons wrong??

Code: Lua
  1. local function PlayerWeapons(ply , OW , NW)
  2.         if OW ~= weapon_physgun then
  3.                 print ("It Worked!")
  4.                 SP = false end
  5.         if OW ~= gmod_tool then
  6.                 SP = false end
  7.         if OW ~= gmod_camera then
  8.                 SP = false
  9.                 print(OW)end
  10. end
  11. hook.Add("PlayerSwitchWeapon", "UName" , PlayerWeapons)

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #2 on: November 17, 2017, 08:12:09 pm »
You're using the Gmod wiki as reference, correct?
In your first example, it seems you're missing possibly using classname.
I'm not able to test at this time, but, you should use print statements in your code to see what's being tested.
Example, what happens in your first example if you use a "print wep" after the wep assignment? Error? String expected, got table?

In your second example, don't just  print "it worked", actually "print OW" before or after all the if statements (after the final end, not before it)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #3 on: November 18, 2017, 12:35:37 pm »
Yes, I am using Gmod wiki. What do you mean by class? I have tried to add the (Class:Weapon) In front of the tool(s) but I get this error...

[ERROR] lua/test.lua:8: attempt to index global 'Class' (a nil value)
  1. v - lua/test.lua:8
   2. unknown - lua/includes/modules/hook.lua:84

I don't know what it means by global Index... Maybe Class is not defined. But yet again it goes back to not knowing how to compare weapon types. I have searched youtube and other places and just can't seem to find something that works. I coming from Java programming so I know a bit of how Lua works but sorry if I sound stupid.

This is how I have been testing it. I have added the Print statements just outside the end in each if and the print(OW)  (Old Weapon) inside. I have also added a print at the end after all if's as well. What I think it is now is that I have the weapon names wrong. That would be the most logical reason for it to go through all the if statements.

Code: Lua
  1. local function PlayerWeapons(ply , OW , NW)
  2.                 print("Before All If's")
  3.         if OW ~= weapon_physgun then
  4.                 SP = false
  5.                 print(OW)end print("Outside if 1")
  6.         if OW ~= gmod_tool then
  7.                 SP = false
  8.                 print(OW)end print("Outside if 2")
  9.         if OW ~= Class:Weapon "gmod_camera" then --The Class:Weapon i used to test if it would work and it throws the error show above
  10.                 SP = false
  11.                 print(OW)end print("Outside if 3")
  12. end
  13. print(OW)       print("Outside all If's")
  14. hook.Add("PlayerSwitchWeapon", "UName" , PlayerWeapons)

I did it without the Class:Weapon and this is what I got on the console when I switched weapons.

First, this is what I got when I ran the code before I even switched weapons.
nil
Outside all If's

Then when I switched my weapon I got this.

Before All If's
Weapon [75][gmod_camera]
Outside if 1
Weapon [75][gmod_camera]
Outside if 2
Weapon [75][gmod_camera]
Outside if 3

As you can see it runs through all the if statements as if all the OW(Old Weapons) do not equal any of the if statements. And I don't know why...

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #4 on: November 18, 2017, 03:29:15 pm »
Pay close attention to the example code lines in GetActiveWeapon - https://wiki.garrysmod.com/page/Player/GetActiveWeapon
Example is even replicated in the Hook PlayerSwitchWeapon - https://wiki.garrysmod.com/page/GM/PlayerSwitchWeapon
They all use GetClass in the print or MsgN statements.
I'm pretty sure you'll be able to compare those results.

GetActiveWeapon contains more (quite apparently now, right?) than _just_ the type, it contains (apparently, guessing here) the fact it's a "Weapon" according to Source (Just because it's Gmod doesn't mean it's not filling a Weapon slot in Source server), the number of the weapon according to (Gmod?), and, the class of that weapon(Source) according to Gmod (gmod_blah)

You can't just make up Globals like you tried to do in your code Class:weapon.
As you somewhat guessed, yes, that's why you get the lua error.

Try this. It's not tested, and not really completed for what entirely you're trying to do, but will hopefully get you closer.
Code: [Select]
local function CheckSwitch(ply , OW , NW)
    oldwep = OW:GetClass()
    newwep = NW:GetClass()
    print("Starting checks...")
    if newwep ~= weapon_physgun then
        print ( "Check 1 - New Weapon was not a physgun, but instead a " .. newwep )
    end
    print("past check 1")
    if newwep ~= gmod_tool then
        print ( "Check 2 - New Weapon was not a tool, but instead a " .. newwep )
    end
    print("past check 2")
    if newwep ~= "gmod_camera" then
        print ( "Check 3 - New Weapon was not a camera, but instead a " .. newwep )
    end
    print("past check 3")
    print("Past all checks")
    print( ply:Nick() .. "just attempted to switch from " .. oldwep .. " to " .. newwep .. ".")
end

hook.Add("PlayerSwitchWeapon", "Check_allow_switch" , CheckSwitch)

And to be clear - you don't sound stupid.
The fact you're here making an effort actually makes us feel warm and fuzzy. :)
« Last Edit: November 18, 2017, 03:36:46 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #5 on: November 19, 2017, 11:28:23 am »
How is your project going?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #6 on: November 19, 2017, 12:16:00 pm »
What you have given me is working great. I have found a way to make it work with what I need. The only thing that I am running into now is that because they are all separate if statements spawn protection is disabled even if I switch to the gmod_tool, or camera, or physics gun. I know that in Java you can do or statements to check multiple conditions with one if statement and i have tried this with the code by doing this.

Code: Lua
  1. local function CheckSwitch(ply , OW , NW)
  2.     oldwep = OW:GetClass()
  3.     newwep = NW:GetClass()
  4.     print("Starting check...")
  5.        
  6.     if newwep ~= "weapon_physgun" or not "gmod_camera" or not "gmod_tool" then
  7.                 v:SetNWBool("SpawnProtected", false)                                                                      --this is for the disabling of the spawn protect
  8.                 print ( ply:Nick() .. "'s New Weapon was not a " .. oldwep ", but instead a " .. newwep )
  9.     end
  10.        
  11.     print("Past checks")
  12.     print( ply:Nick() .. " just attempted to switch from " .. oldwep .. " to " .. newwep .. ".")
  13. end
  14.  
  15. hook.Add("PlayerSwitchWeapon", "Check_allow_switch" , CheckSwitch)

This works but not with the "or not" conditions that I have added. When I switch to then physics gun it skips the if and does what I need it to, but when I go to the other two it still runs the if statement.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #7 on: November 19, 2017, 12:55:02 pm »
Though you can use operators in an if statement;
1) you can't do it the way you're trying
- you must compare the variable each time. I'm 95% sure you can't use short circuit if comparisons, even though you can use short circuit variable assignments
2) You're mixing comparison methods. Don't use "a ~= b" along with "not b == c".
- Sure, both would work, but pick a style and flow through out your code.

My personal preference when learning is to make code "readable", or in my case, as close to english as possible.
So, try this line out for your line 6 in your example
Code: [Select]
if not newwep == "weapon_physgun" or not newwep == "gmod_camera" or not newwep == "gmod_tool" thenIf you prefer, and using a few characters less (which in real world large project coding, can save hundreds of bytes of course)
Code: [Select]
if newwep ~= "weapon_physgun" or newwep ~= "gmod_camera" or newwep ~= "gmod_tool" then
Both code lines would perform same check.

If I'm wrong about short circuit comparisons, you might get this to work; (feel free to try it, no, please do).
Code: [Select]
if not newwep == "weapon_physgun" or "gmod_camera" or "gmod_tool" then
« Last Edit: November 19, 2017, 12:57:50 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #8 on: November 19, 2017, 01:38:26 pm »
Ha, Noice. This works. So, I tried the different ways that you have shown and I went with the second to last one( a bit Shorter one). The only thing that I saw was that the "or" did not work. When I had the "or" there it would run the code, no matter what. So I replaced the "or" with a "and" and now it runs perfectly. When I switch to any of the weapons that are listed in the code it skips the if and keeps going. *Horray!  ;D!*

Code: Lua
  1.         if newwep ~= "weapon_physgun" and newwep ~= "gmod_camera" and newwep ~= "gmod_tool" then

Now all that is left to do is add it to all together and see if it works!. Hopefully, it does.

EDIT: It did not.  :-[ There are no errors in the code but it seems like my code that I have added does not even run at all. I am testing this on a real server that I own and I don't know if that makes a difference. I did put it into the server autorun file, but still nothing. I tried to drop it into a for loop that was already there that looked like it was for checking if the player should be spawn protected or not but it did not work either.
« Last Edit: November 19, 2017, 06:36:23 pm by Reaper_007_ »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #9 on: November 19, 2017, 06:50:15 pm »
Though it's possible you do it elsewhere that I don't see, you aren't setting 'v' in v:SetNWBool.
(Though, I'd be surprised if there's not a server console error stating V is nil or invalid entity)

Is that a variable that's supposed to be attached to player "ply" entity?

« Last Edit: November 19, 2017, 06:52:33 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #10 on: November 19, 2017, 07:05:19 pm »
This is part of an addon that is complete. That is where the "v" is coming from. The "v" was defined further up by the coder who made the addon and he uses it a few times in that format (That is why I thought that is how he was turning off spawn protection when need be). Though, I did find a simple way of disabling spawn protection and am now using it instead. I will not be using   "v:SetNWBool("SpawnProtected", false)" anymore. I can send you the complete add-on if you would like to look at the code and see where I added my code into, But this is what my code now looks like. Again there are no errors, might be because it not running it at all, but I don't know where to properly place my code in order for it to run constantly and see if a player on the server is switching weapons.
Code: Lua
  1. local function CheckSwitch(ply , OW , NW)                               --Added code starts here
  2.     oldwep = OW:GetClass()
  3.     newwep = NW:GetClass()
  4.     print("Starting check...")
  5.         if newwep ~= "weapon_physgun" and newwep ~= "gmod_camera" and newwep ~= "gmod_tool" then
  6.                 ply.PreviouslyProtected = true
  7.                
  8.                 print ( ply:Nick() .. "'s New Weapon was not a " .. oldwep .. ", but instead a " .. newwep )
  9.                 print (ply:Nick() .. " Spawn Protection has been set to " .. SpawnProtected )
  10.     end
  11.         hook.Add( "PlayerSay", "Checkwep", function( ply, text, public ) --Check if code is even being ran at all
  12.         if ( string.lower( text ) == "!wep" ) then
  13.                 print(oldwep)
  14.                 return "[Global]" .. oldwep
  15.         end
  16. end )
  17.        
  18.     print("Past checks")
  19.     print( ply:Nick() .. " just attempted to switch from " .. oldwep .. " to " .. newwep .. ".")
  20. end
  21. end
  22. hook.Add("PlayerSwitchWeapon", "Check_allow_switch" , CheckSwitch)                      --Added code ends here
The "ply.PreviouslyProtected = true" is a part of the addon where if a player leaves spawn and comes back in he will not be protected again and I am using that to make it seem like when a player switches weapons to make the code think that he has left spawn and came back in. And that way spawn protection will be disabled.
« Last Edit: November 19, 2017, 07:09:27 pm by Reaper_007_ »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #11 on: November 20, 2017, 03:58:55 pm »
RE: Where to put file - reference https://wiki.garrysmod.com/page/Beginner_Tutorial_Intro
Place your file in garrysmod\lua\autorun\server, unless the mod you're working with has a recommendation already.
(Example, our ULib library, and ULX admin mod have a modules folder similar to the Gmod autorun - difference? Our folders only load modules after our libraries and functions have been initialized. A function written for ULib could error if a Ulib command was called on the server before ULib had loaded (same for ULX, before ULX loaded)

Re: Ensure code is running constantly
1) A) You can't test this in a single player game. Reference the very top of the PlayerSwitchWeapon hook - it's a predicted hook that requires server comms - not run in client domain unless on server too.
    B) I _think_ this could be tested on a listen server (server started while in game client, not srcds)
2) It's a hook - Hooks are always 'running', that is, trying to hook into a game event, in this case, a weapon switch. They do have 'domains' - server, client, shared. This hooks is a shared hook, with a limitation (see #1A)

LOGIC
I see a challenge here.
Let me verify - you want to make sure the person being spawned is ONLY switching to a tool, camera, or physgun, right?
Think TRUE or FALSE.
With your IF statement the way it is now, _ANY_ item not matching the other two comes out false.

if newwep ~= "weapon_physgun" and newwep ~= "gmod_camera" and newwep ~= "gmod_tool" then
Say I switch to a camera.
(true and false and true) == false
switch to physgun
(false and true and true) == false
switch to any tool
(true and true and false) == false

Try checking to see if newwep IS any of the 3, and use OR, but with a negate in front of the check, and parenthesis to group.
if not (newwep == "weapon_physgun" or newwep == "gmod_camera" or newwep == "gmod_tool") then
physgun
not (true or false or false) == false (not true = false)
camera
not (false or true or false) == false (not true = false)
tool
not (false or false or true) == false (not true = false)
WEAPON
not (false or false or false) == TRUE (not false = true - KILL PROTECTION)

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Reaper_007_

  • Newbie
  • *
  • Posts: 11
  • Karma: 1
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #12 on: November 20, 2017, 06:43:13 pm »
Yes, i am checking if the player spawned is switching between the weapons. BUT, I am checking that ANYTHING other than those 3 weapons that they spawn will make them lose their spawn protection. I have got it working now and it disables their spawn protection when they take out a gun (That is what I needed).

First, I want to thank you for all your help. I would not have got it done without you. I will be continuing to learn Lua because my end goal is to be able to make my own mods for my servers and modify mods if I need. I'm sure I will be back looking for help again lol. Thanks again!

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need Help Finding and Comparing Weapons That A Player Is Holding
« Reply #13 on: November 20, 2017, 07:25:44 pm »
Welcome. Small steps - looks like your making good learning process.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print