Recent Posts

Pages: [1] 2 3 ... 10
1
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
2
Off-Topic / M9K Weapons List & Ammo Spreadsheet
« Last post by BraveNM22 on Today at 12:18:53 PM »
 idk if this needs to go in off-topic, but hey! thought some people would want this. I know i really did, so i made my own.
https://docs.google.com/spreadsheets/d/1U0Tn7d4kkcSbvuI8YlkBwK8S1GnJxkXLP2Fy0FBNV2s/edit?usp=sharing
its anonymous so no need to worry. Thanks for checking this out!


oh and http://www.civilgamers.com/mobile/forum/viewthread/m/18343296/id/16378986-every-m9k-gun-file-for-budding-coders really helped me out in making this so check that out too  :)
3
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.
4
How is your project going?
5
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. :)
6
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...
8
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)
9
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)
10
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.  
Pages: [1] 2 3 ... 10