• Print

Author Topic: stool and weapon restriction  (Read 19524 times)

0 Members and 1 Guest are viewing this topic.

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
stool and weapon restriction
« on: May 30, 2008, 08:25:23 am »
Hello everyone,
I have been trying to learn all of this lua coding to have my server run the way I want it. I am trying to make a lua code that will restrict the use of a stool and weapons to certain user groups.
I would like to have it so that others may use my codes and maybe turn it into an addon. That will come later, so here we go.
We will start with the stool restrictions 1st then progress.

My User Groups
SuperAdmin, Admin, Moderator, VIP, Respected, Regulars, and Guest. (in that order)

stools to be restricted: anti-noclip, light including wire, lamp including wire, ignite including wire, rt cam, trails including wire, balloons, smart weld, emitter including wire, stacker, turret including wire, dynamite including wire.

Group allows:
SuperAdmin/Admin:  all restricted stools above
Moderator:
VIP:                       Balloons, smart weld, wire ignite, emitter.
Respected:             Stacker, turret including wire, dynamite including wire.
Regulars:
Guest:

My code:

Code: [Select]
function UseTool( ply, tr, toolmode )
if toolmode == "rt_antinoclip" or toolmode == "light" or toolmode == "lamp" or toolmode == "ignite"  // put the FILE NAME of the tool
or toolmode == "rtcamera" or toolmode == "trails" or toolmode == "wire_light" or toolmode == "wire_lamp" then //
if ( ply:IsAdmin() or ply:IsSuperAdmin()  )then
return true
else
if !ply:IsAdmin() and !ply:IsSuperAdmin() then
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" )
return false
end
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

function UseTool( ply, tr, toolmode )
if toolmode == "balloon" or toolmode == "smartwelder" or toolmode == "wire_igniter" or toolmode == "emitter" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") )then
return true
else
if !ply:IsAdmin() and !ply:IsSuperAdmin() !ply:IsUserGroup("moderator") and !ply:IsUserGroup("vip") then
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins, Moderators, and VIPs." )
return false
end
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

function UseTool( ply, tr, toolmode )
if toolmode == "stacker" or toolmode == "turret" or toolmode == "dynamite" or toolmode == "wire_turret" or toolmode == "wire_explosive" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") or ply:IsUserGroup("respected") )then
return true
else
if !ply:IsAdmin() and !ply:IsSuperAdmin() !ply:IsUserGroup("moderator") and !ply:IsUserGroup("vip") and !ply:IsUserGroup("respected") then
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above." )
return false
end
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

Let me know what you think,
Saint Mark

« Last Edit: May 31, 2008, 02:23:44 am by JamminR »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: stool and weapon restriction
« Reply #1 on: May 30, 2008, 12:07:18 pm »
the second check in each function is redundent. In the else part of the IsAdmin check you don't need if !ply:IsAdmin because it will never be true anyways.

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
Re: stool and weapon restriction
« Reply #2 on: May 30, 2008, 08:30:18 pm »
the second check in each function is redundent. In the else part of the IsAdmin check you don't need if !ply:IsAdmin because it will never be true anyways.

So what you are saying is do it like this:

Code: [Select]
function UseTool( ply, tr, toolmode )
if toolmode == "rt_antinoclip" or toolmode == "light" or toolmode == "lamp" or toolmode == "ignite"  // put the FILE NAME of the tool
or toolmode == "rtcamera" or toolmode == "trails" or toolmode == "wire_light" or toolmode == "wire_lamp" then //
if ( ply:IsAdmin() or ply:IsSuperAdmin()  )then
return true
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" )
return false

end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: stool and weapon restriction
« Reply #3 on: May 30, 2008, 09:01:44 pm »
Instead of returning true, just return. As someone on this forum told me, if you return true you could break other scripts. Other then that, it looks good to me.

==EDIT==
JamminR:
Quote
A wise coder once told me, never return a non-nil value unless your calling a function expecting a non-nil value.
IE, don't use 'return true', just use 'return'. Return false should work fine though.
As for your only working once with more than one dlist item, no idea.
Megiddo:
Quote
If you return true you're telling GMod to ignore all other hooks after yours. Breakage++

Just passing on the wise thoughts :D
« Last Edit: May 30, 2008, 09:46:03 pm by jay209015 »
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
Re: stool and weapon restriction
« Reply #4 on: May 30, 2008, 11:32:21 pm »
Thanks for pointing those things out.

Here is the full code with the corrections made.
See if there is anything else that could be miscoded.

mycode1.1

Code: [Select]
function UseTool( ply, tr, toolmode )
if toolmode == "rt_antinoclip" or toolmode == "light" or toolmode == "lamp" or toolmode == "ignite"  // put the FILE NAME of the tool
or toolmode == "rtcamera" or toolmode == "trails" or toolmode == "wire_light" or toolmode == "wire_lamp" then 
if ( ply:IsAdmin() or ply:IsSuperAdmin()  ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" )
return false
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

function UseTool( ply, tr, toolmode )
if toolmode == "balloon" or toolmode == "smartwelder" or toolmode == "wire_igniter" or toolmode == "emitter" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins, Moderators, and VIPs." )
return false
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

function UseTool( ply, tr, toolmode )
if toolmode == "stacker" or toolmode == "turret" or toolmode == "dynamite" or toolmode == "wire_turret" or toolmode == "wire_explosive" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") or ply:IsUserGroup("respected") ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above." )
return false
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

Also would the code work if it was written like the below format? I thought if it was written like this it would be much easier for people to edit it the way they want it.

ex.A
Code: [Select]
function UseTool( ply, tr, toolmode )
if toolmode == "stacker"          // put the FILE NAME of the tool
or toolmode == "turret"          // If you need more, just copy the format here
or toolmode == "dynamite"       // You can delete one or more if you do not need them
or toolmode == "wire_turret"
or toolmode == "wire_explosive"
then
if ( ply:IsAdmin()
or ply:IsSuperAdmin()             // place what groups you want to be allowed to use above stools
or ply:IsUserGroup("moderator")  // If you need more, just copy the format here
or ply:IsUserGroup("vip")       // You can delete one or more if you do not need them
or ply:IsUserGroup("respected") ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above." )
return false
end
end
end
hook.Add( "CanTool", "UseTools", UseTool )

Tested on my server - failed.
I set myself as a Guest and tried to use all the restricted tolls. the only ones that worked were dynamite including wire and turret including wire. When i clicked to use the tool it gave me two PrintMessages - "This tool is restricted to Respected Players and above."
Im stumped. See if you know where I sent wrong.
« Last Edit: May 31, 2008, 04:47:13 am by saintmark »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: stool and weapon restriction
« Reply #5 on: May 31, 2008, 05:46:31 am »
After spending 20 minutes on writing a nice explanation of why it might not be working, I realized you're probably trying your 3 Cantool hooks, not just one like I thought at first.
One for each list of tools./group of admins.

You can't name your hook the same.
Rename your hooks, the 2nd in hook.Add(Hooktype, "unique name - this one", function name"

You can't name your functions the same.
Rename your functions, anything you want, but not "UseTool". Something like UseTool_<unique name> for each function.
« Last Edit: May 31, 2008, 06:54:31 am by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
Re: stool and weapon restriction
« Reply #6 on: May 31, 2008, 06:10:01 pm »
Ok I renamed the functions to:
1st: UseTool_admin
2nd: UseTool_vip
3rd: Use Tool_respected

I do not understand how I should do the hooks, would you please show me an example using the information above?

Just to make sure we are all on the same page, I am making one lua file that has all the codes in it for stool restriction. This can be done right?

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: stool and weapon restriction
« Reply #7 on: May 31, 2008, 06:17:22 pm »
like this:
Code: [Select]
function UseTool_admin( ply, toolmode )
        contents....................
     
end
hood.Add( "CanTool", "UseTool_Admin_Hook", UseTool_admin)

function UseTool_vip( ply, toolmode )
        contents....................
     
end
hood.Add( "CanTool", "UseTool_vip_Hook", UseTool_vip)

function UseTool_respected( ply, toolmode )
        contents....................
     
end
hood.Add( "CanTool", "UseTool_respected_Hook", UseTool_respected)


An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: stool and weapon restriction
« Reply #8 on: May 31, 2008, 06:25:22 pm »
A hook is very easy to understand once you know what it means.

in the hook command...

hook.Add

The first parameter is the type of hook it is.. this is set by garry(lua) and can not change. For you.. it's CanTool
The second parameter is what you PERSONALLY want to name it. It HAS to be unique and this is how you could later on remove the hook or whatever.
The third and final parameter is which function you created you are hooking. This has to be the name of the function and lua is CaSe SeNsAtIvE.

you do not HAVE to hook a function after you declare it.(However it IS good practice.)

This would be perfectly legal..

Code: Lua
  1.  
  2. function MyFunctionA(ply, toolmode)
  3.      blah blah blah!
  4. end
  5.  
  6. function MyFunctionB(ply, toolmode)
  7.      blah blah blah!
  8. end
  9.  
  10. function MyFunctionC(ply, toolmode)
  11.      blah blah blah!
  12. end
  13.  
  14. hook.Add("CanTool", "MyFunctionA", MyFunctionA)
  15. hook.Add("CanTool", "MyFunctionB", MyFunctionA)
  16. hook.Add("CanTool", "MyFunctionC", MyFunctionA)
  17.  

however... you cant hook a function until AFTER it is declared.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: stool and weapon restriction
« Reply #9 on: May 31, 2008, 06:31:36 pm »
A bit of history... the primary reason why hook.Add() has the second "name" parameter is so that if you reload the script, it's still only hooked once. This was a major issue in GM9. :)
Experiencing God's grace one day at a time.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: stool and weapon restriction
« Reply #10 on: May 31, 2008, 06:52:19 pm »
Nice! This is good to know actually, i was always wondering if when I reloaded a script without restarting my server if the function was hooking more than once. I guess now I know! =)

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
Re: stool and weapon restriction
« Reply #11 on: May 31, 2008, 09:24:47 pm »
Great info and help guys THANKS!!! ;D

I have good news too.... my server was empty so I had a chance to test it out -IT WORKED!!!!!!
I am soooooo very happy. I am proud of my little code. The only thing I cant figure out is why it double prints the HUD_PRINTTALK

my code ver1.2

Code: [Select]
function UseTool_admin( ply, tr, toolmode )
if toolmode == "rt_antinoclip" or toolmode == "light" or toolmode == "lamp" or toolmode == "ignite"  // put the FILE NAME of the tool
or toolmode == "rtcamera" or toolmode == "trails" or toolmode == "wire_light" or toolmode == "wire_lamp" then 
if ( ply:IsAdmin() or ply:IsSuperAdmin()  ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" )
return false
end
end
end
hook.Add( "CanTool", "UseTool_admin_Hook", UseTool_admin )

function UseTool_vip( ply, tr, toolmode )
if toolmode == "balloon" or toolmode == "smartwelder" or toolmode == "wire_igniter" or toolmode == "emitter" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins, Moderators, and VIPs." )
return false
end
end
end
hook.Add( "CanTool", "UseTool_vip_Hook", UseTool_vip )

function UseTool_respected( ply, tr, toolmode )
if toolmode == "ol_stacker" or toolmode == "turret" or toolmode == "dynamite" or toolmode == "wire_turret" or toolmode == "wire_explosive" then // put the FILE NAME of the tool
if ( ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") or ply:IsUserGroup("respected") ) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above." )
return false
end
end
end
hook.Add( "CanTool", "UseTool_respected_Hook", UseTool_respected )

Also nobody posted if I could write my codes like in ex A. I think it looks cleaner and it would be easy for someone to modify.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: stool and weapon restriction
« Reply #12 on: June 01, 2008, 02:15:06 pm »
Since you wanted an easier configuration of the tools, I put them into three tables. Not test, but I think it should work.

Code: [Select]
// List of admin only tools
Admin_Tools = { "rt_antinoclip", "light", "lamp", "ignite", "rtcamera", "trails", "wire_light", "wire_lamp" }
// List of V.I.P. tools
VIP_Tools = { "balloon", "smartwelder", "wire_igniter", "emitter" }
// List of Respected tools
Respected_Tools = { "ol_stacker", "turret", "dynamite", "wire_turret", "wire_explosive" }

//========================================END OF CONFIGURATION===========================================//



function UseTool_admin( ply, tr, toolmode )
for _, toolA in pairs(Admin_Tools) do // Loops through the Admin_Tools table
if toolmode == toolA then // Checks to see if the toolmode equals any of the tools in that table
if ply:IsAdmin() or ply:IsSuperAdmin() then // checks to see if they are in the selected groups or not
return // If they are then allow the use of the tool
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" ) // If not admin then prints the message "This tool is restricted to Admins"
return false // And disallows the use of the tool for that player
end
end
end
end
hook.Add( "CanTool", "UseTool_admin_Hook", UseTool_admin )

function UseTool_vip( ply, tr, toolmode )
for _, toolV in pairs(VIP_Tools) do // Loops through the VIP_Tools table
if toolmode == toolV then // Checks to see if the toolmode equals any of the tools in that table
if ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") then // checks to see if they are in the selected groups or not
return // If they are then allow the use of the tool
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" ) // If not then prints the message "This tool is restricted to Admins, Moderators, and VIPs."
return false // And disallows the use of the tool for that player
end
end
end
end
hook.Add( "CanTool", "UseTool_vip_Hook", UseTool_vip )

function UseTool_respected( ply, tr, toolmode )
for _, toolR in pairs(Respected_Tools) do // Loops through the VIP_Tools table
if toolmode == toolR then // Checks to see if the toolmode equals any of the tools in that table
if ply:IsAdmin() or ply:IsSuperAdmin() or ply:IsUserGroup("moderator") or ply:IsUserGroup("vip") or ply:IsUserGroup("respected") then // checks to see if they are in the selected groups or not
return // If they are then allow the use of the tool
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above" ) // If not then prints the message "This tool is restricted to Respected Players and above"
return false // And disallows the use of the tool for that player
end
end
end
end
hook.Add( "CanTool", "UseTool_vip_Hook", UseTool_vip )
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline saintmark

  • Newbie
  • *
  • Posts: 27
  • Karma: 1
Re: stool and weapon restriction
« Reply #13 on: June 01, 2008, 03:30:19 pm »
Wow Jay That looks great and easy to mod... You went above the call of duty!

Your code looks better than mine.

my code ver 1.3

Code: [Select]
   // This code was written by Saint Mark - with the assistance from the lua kings of www.ulyssesmod.net
  // You can have as many UseTool_group's as you want (Copy and Paste down below)--
 // --Just make sure you change the function UseTool_groupA to another letter -
//-Also make sure you change it in the -hook.Add( "CanTool", "UseTool_groupA_Hook", UseTool_groupA ) -


function UseTool_groupA( ply, tr, toolmode )    //This is the first line of groupA 
if toolmode == "rt_antinoclip"           //place the FILE NAME of the tool inside the quotes
or toolmode == "light"                  // You may add or delete these too
or toolmode == "lamp"
or toolmode == "ignite"       
or toolmode == "rtcamera"
or toolmode == "trails"
or toolmode == "wire_light"
or toolmode == "wire_lamp"
then 
if ( ply:IsAdmin()
or ply:IsSuperAdmin() 
) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins" ) //you can change the text that will be printed on the screen
return false
end
end
end
hook.Add( "CanTool", "UseTool_groupA_Hook", UseTool_groupA ) //This is the last line of GroupA

function UseTool_groupB( ply, tr, toolmode )
if toolmode == "balloon"
or toolmode == "smartwelder"
or toolmode == "wire_igniter"
or toolmode == "emitter"
then
if ( ply:IsAdmin()
or ply:IsSuperAdmin()
or ply:IsUserGroup("moderator")
or ply:IsUserGroup("vip")
) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Admins, Moderators, and VIPs." )
return false
end
end
end
hook.Add( "CanTool", "UseTool_groupB_Hook", UseTool_groupB )

function UseTool_groupC( ply, tr, toolmode )
if toolmode == "ol_stacker"
or toolmode == "turret"
or toolmode == "dynamite"
or toolmode == "wire_turret"
or toolmode == "wire_explosive"
then
if ( ply:IsAdmin()
or ply:IsSuperAdmin()
or ply:IsUserGroup("moderator")
or ply:IsUserGroup("vip")
or ply:IsUserGroup("respected")
) then
return
else
ply:PrintMessage( HUD_PRINTTALK, "This tool is restricted to Respected Players and above." )
return false
end
end
end
hook.Add( "CanTool", "UseTool_groupC_Hook", UseTool_groupC )

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: stool and weapon restriction
« Reply #14 on: June 01, 2008, 03:40:28 pm »
Thanks, either way you want to run this script works :D
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

  • Print