General > Developers Corner

stool and weapon restriction

(1/4) > >>

saintmark:
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: ---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 )

--- End code ---

Let me know what you think,
Saint Mark

MrPresident:
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.

saintmark:

--- Quote from: MrPresident 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.

--- End quote ---

So what you are saying is do it like this:


--- Code: ---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 )
--- End code ---

jay209015:
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.
--- End quote ---
Megiddo:

--- Quote ---If you return true you're telling GMod to ignore all other hooks after yours. Breakage++
--- End quote ---

Just passing on the wise thoughts :D

saintmark:
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: ---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 )

--- End code ---

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: ---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 )
--- End code ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version