!= to compare strings?

Hi, I’m working on a new gamemode for practice and for fun and I’ve run into a problem. I’ve got the following code:


function GM:PlayerSwitchWeapon( ply, oldWeapon, newWeapon )
    if newWeapon:GetClass() != "weapon_physcannon" or "weapon_physgun" or "gmod_tool" then
        ply:GodDisable()
        else
        return false
    end
end



However, whenever I switch my weapon to grav gun or tool gun, it disables god. Same with any other weapon too. What am I doing wrong?
Thanks!

that’s not how logic operators work

[lua]if newWeapon:GetClass() ~= “weapon_physcannon” and newWeapon:GetClass() ~= “weapon_physgun” and newWeapon:GetClass() ~= “gmod_tool” then[/lua]

you could also use a table

[lua]
local weapons = {[“weapon_physcannon”] = true, [“weapon_physgun”] = true, [“gmod_tool”] = true}

if not(weapons[newWeapon:GetClass()]) then
[/lua]

[editline]a[/editline]

code_gs is right, should be and, not or

-snip- im dumb thanks for the help guys!

You should be using and since or will pass if one element is true. Also, you can’t test them in a series like that when using a != operator; you must test each one.

Your code:


function PlayerSwitchWeapon( ply, oldWeapon, newWeapon )
    if newWeapon ~= "weapon_physcannon" or "weapon_physgun" or "gmod_tool" then
        print"passed" else print"didn\'t pass"
    end
end

PlayerSwitchWeapon( "", "", "weapon_physcannon")
PlayerSwitchWeapon( "", "", "weapon_physgun")
PlayerSwitchWeapon( "", "", "gmod_tool")
PlayerSwitchWeapon( "", "", "weapon_yeah")

Output:


passed
passed
passed
passed

New code:


function PlayerSwitchWeapon( ply, oldWeapon, newWeapon )
    if newWeapon ~= "weapon_physcannon" and newWeapon ~= "weapon_physgun" and newWeapon ~= "gmod_tool" then
        print"passed" else print"didn\'t pass"
    end
end

PlayerSwitchWeapon( "", "", "weapon_physcannon")
PlayerSwitchWeapon( "", "", "weapon_physgun")
PlayerSwitchWeapon( "", "", "gmod_tool")
PlayerSwitchWeapon( "", "", "weapon_yeah")

Output:


didn't pass
didn't pass
didn't pass
passed

[editline]5th January 2015[/editline]

Comment that out and try again; do they still turn pink?