Author Topic: stool and weapon restriction  (Read 9495 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: 2728
  • 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

An Error Has Occurred!

array_keys(): Argument #1 ($array) must be of type array, null given