• LUA SWEP Help.
    4 replies, posted
Hello! I'm trying to make a code that restricts a weapon to a ULX group. I am currently trying to restrict MK9 Heavy Weapons to the ULX group 'Donator' in the shared.lua. I am an intermediate lua coder, and I tried my own code but it did not work. Here is my failed code: [CODE]function denySwep( ply ) if ply:IsUserGroup("owner") then ply:ChatPrint ("You must be a donator to use these weapons! Please visit samg381.com to donate!") return false end end [/CODE] Could anyone fix this for me? Also, since I am learning, if you would like please add an explanation to what you did! Thank you :v:
this basically says that if you're an owner rank, than to return false. Here's what i'd do [lua] function denySwep( ply ) if ply:IsUserGroup("donator") or ply:IsUserGroup("admin") or ply:IsUserGroup("superadmin") or ply:IsUserGroup("owner") then return false else ply:ChatPrint("you must be a donator to use this weapon!") return true end end --if !denyswep(ply) then (give weapons or whatever here) [/lua]
[QUOTE=Archemyde;40249195]this basically says that if you're an owner rank, than to return false. Here's what i'd do [lua] function denySwep( ply ) if ply:IsUserGroup("donator") or ply:IsUserGroup("admin") or ply:IsUserGroup("superadmin") or ply:IsUserGroup("owner") then return false else ply:ChatPrint("you must be a donator to use this weapon!") return true end end --if !denyswep(ply) then (give weapons or whatever here) [/lua][/QUOTE] Thank you sir.
It's generally good programming etiquette to make boolean (true / false) functions always named positively, rather than negatively. This is what I would do (although both will accomplish the same thing) [lua]function canUseSwep( ply ) allowedGroups = { "donator", "admin", "superadmin", "owner" } for k, v in pairs( allowedGroups ) do if ply:IsUserGroup( v ) then return true end end ply:ChatPrint( "You must be a donator to use this SWEP!" ) return false end --if canUseSwep( ply ) then give weapons [/lua]
[QUOTE=TheCloak;40267289]It's generally good programming etiquette to make boolean (true / false) functions always named positively, rather than negatively. This is what I would do (although both will accomplish the same thing) [lua] local allowedGroups = { "donator", "admin", "superadmin", "owner" } function canUseSwep( ply ) return table.HasValue(allowedGroups,ply:GetUserGroup()) end [/lua][/QUOTE] In this case you could use table.HasValue, a gmod specific function which is quite useful (I have changed your code to reflect this).
Sorry, you need to Log In to post a reply to this thread.