Permisions for certain ID's

Greetings everyone.
I am currently working on making a give weapon system, in witch you click a derma button and it gives you a weapon.
My question is, how would i make it so only certain ID’s can use it.
Thanks.

It depends what the ID is, if you’re using the player’s Unique ID then you could do something like this:
[lua]
local weaponAllowed = {
[weapon1] = {
[id1] = true,
[id2] = false,

[idn] = false,
},
}
local function CanGiveWeapon(player, weapon)
return weaponAllowed[weapon][player:UniqueID()]
end

local function SetAllowedWeapon(player, weapon, cangive)
if not weaponAllowed[weapon] then
weaponAllowed[weapon] = {}
end
weaponAllowed[weapon][player:UniqueID()] = cangive
end
[/lua]

Or couldn’t you do something like:

[lua]

function meta:CanWeapon()

	if( self:SteamID() == "STEAMID here"
                            or self:SteamID() == "STEAMID here"

	return true
end

return false

end
[/lua]

Then you can do if ply:IsCanWeapon() then

Setup a table of SteamIDs and do a check if the steamid is in the table.

ie.

[lua]
CanSpawnWeapon =
{

"steamvsvcxv",
 "steamvsvcxv",
"steamvsvcxv",

}

– In your derma function do something like this

for k, id in pairs( CanSpawnWeapon )

 if( LocalPlayer():SteamID() == id ) then

        --code

 end

end[/lua]

I was bored, so I wrote this up:
[lua]
– Set Permission script for Garry’s Mod
– Copyright 2009 Declan White
– Licensed under Creative Commons Attribution-Noncommercial-Share Alike 2.5
– (creativecommons.org/licenses/by-nc-sa/2.5/)

function player.FindByName(pattern, ignore_case, plain, exact)
pattern = ignore_case and string.lower(pattern) or pattern
local best_count, best, start_pos, end_pos = 0
for k,ply in pairs(player.GetAll()) do
if exact then
if (ignore_case and string.lower(ply:GetName()) or ply:GetName()) == pattern then
return ply
end
else
start_pos, end_pos = string.find(
ignore_case and string.lower(ply:GetName()) or ply:GetName(),
pattern,
1,
plain
)
if start_pos and end_pos and end_pos-start_pos > best_count then
best = ply
best_count = end_pos-start_pos
end
end
end
return best
end
function player.GetByName(name, ignore_case)
return player.FindByName(name, ignore_case, nil, true)
end
function player.GetBySteamID(steamid)
u_part, t_part, id_part = string.match(steamid, “^[Ss]?[Tt]?[Ee]?[Aa]?[Mm]??(%d):?(%d):?(%d+)$")
if not u_part or not t_part or not id_part then return end
steamid = "STEAM
”…u_part…":"…t_part…":"…id_part
for k,ply in pairs(player.GetHumans()) do
if steamid == ply:SteamID() then
return ply
end
end
end
function player.GetByUserID(userid)
userid = tonumber(userid)
for k,ply in pairs(player.GetAll()) do
if ply:UserID() == userid then
return ply
end
end
end

function R.Player:HasPermission(permission)
return self:GetPData("permission
"…string.lower(permision))
end

local function CC_set_permission(ply, cmd, args) – I made this to account for CONSOLE being ‘invalid’, but some admin mods change this
if ply and ply:IsValid() and not ply:IsAdmin() then – you can change this to ply:IsSuperAdmin() if you want
ply:ChatPrint(“Access denied!”)
return end
local name = args[1]
if not name then
if not (ply and ply:IsValid()) then
Msg("Name, SteamID, UniqueID or UserID expected for first argument of set_permission.
")
else
ply:ChatPrint(“Name, SteamID, UniqueID or UserID expected for first argument of set_permission.”)
end
return end
target = player.FindByName(name, true) – Feel free to rearrange these at will.
or player.GetBySteamID(name)
or player.GetByUniqueID(name)
or player.GetByUserID(name)
if not (target and target:IsValid()) then
if not (ply and ply:IsValid()) then
Msg(“Unable to find player from “…tostring(name)…” (need name, SteamID, UniqueID or UserID).
“)
else
ply:ChatPrint(“Unable to find player from “…tostring(name)…” (need name, SteamID, UniqueID or UserID).”)
end
return end
– If you use ULX or some other admin mod, you may want to do a check here using their special functions.
local permission = args[2]
if not permission or #tostring(permission) == 0 then
if not (ply and ply:IsValid()) then
Msg(“Permission name expected for second argument of set_permission.
“)
else
ply:ChatPrint(“Permission name expected for second argument of set_permission.”)
end
return end
permission = string.lower(permission)
local current_value = target:GetPData(“permission_”…permission)
local set_as = util.tobool(args[3])
if args[3] == nil then
if not (ply and ply:IsValid()) then
Msg(“Boolean value (1, 0, true or false) expected for third argument of set_permission.
(Current value is “…tostring(current_value)…”)
“)
else
ply:ChatPrint(“Boolean value (1, 0, true or false) expected for third argument of set_permission.
(Current value is “…tostring(current_value)…”)”)
end
return end
if util.tobool(current_value) == set_as then
if not (ply and ply:IsValid()) then
Msg(string.format(”%q (%s) has already been %s access to permission %q!
“,
target:GetName(), target:SteamID(),
set_as and “allowed” or “denied”,
permission
))
else
ply:ChatPrint(string.format(”%q (%s) has already been %s access to permission %q!”,
target:GetName(), target:SteamID(),
set_as and “allowed” or “denied”,
permission
))
end
target:SetPData(“permission_”…permission, set_as) – set it anyway, just in case
return end
local msg = string.format(”%q (%s) %s %q (%s) access to permission %q.”,
ply:GetName(), ply:SteamID(),
set_as and “allowed” or “denied”,
target:GetName(), target:SteamID(),
permission
)
Msg(msg…”
“) ServerLog(msg)
target:SetPData(“permission_”…permission, set_as)
if not (ply and ply:IsValid()) then
Msg(string.format(”%q (%s) has been %s access to permission %q.
“,
target:GetName(), target:SteamID(),
set_as and “allowed” or “denied”,
permission
))
else
ply:ChatPrint(string.format(”%q (%s) has been %s access to permission %q.”,
target:GetName(), target:SteamID(),
set_as and “allowed” or “denied”,
permission
))
end
end
concommand.Add(“set_permission”, CC_set_permission)
[/lua]

Enjoy.

Use “ply:HasPermission(‘can_spawn_weapon’)” to check if someone has a permission.
Use “set_permission [NAME/STEAMID/UNIQUEID/USERID] [PERMISSION] [1/0/true/false]” to give or take permissions (requires admin status).

Not sure but.

[lua]
if( self:SteamID() == “STEAMID here”
or self:SteamID() == “STEAMID here”
then BUTTON:SetDisabled (False) else BUTTON:SetDisabled (True)
end
[/lua]

 end

I made a mistake in my code, but it’s fixed now.

Stupid idea.
Never trust the client with permission checks.
Unless the server had a check as well (meaning this check is purely for aesthetics, in which I apologise) anyone could bypass it with by running the command that the button runs.

Also, “hard coding” the IDs into the script is silly. It’s hard to change them and the server has to be restarted each time (as well as every client has to download the file each time it changes).

If you don’t want to use my script, at least learn from it.

Note: my script is purely server-side. If you want to client to know, send them a user message or set a Networked Variable.

Cool. You shared a copyrighted code snippet.

:jerkbag:

Perhaps his name is Declan White?

.But would that of worked???

He was referring to the copyright. Itself.

Here’s some useful code, but you can’t have it.

Forgot to put the licensing note.

Now you can enjoy.

lol, copyrighting lua scripts.

If I take that code, are you going to sue me for copyright infringement for stealing your Permissions script?

No, but you’ll feel like an asshole, and you’ll get a shitstorm from the community (as long as I don’t act like Conna did).

You can actually use it how you wish, due to the license.
If you start selling it in a commercial work, however (I sincerely doubt it), then I will take up legal prosecution.

I don’t see the difference between that and just adding your name on it, in the case.

Because ‘just adding my name on it’ makes it assume the Copyright license. Even just releasing it implies that license.
You have to ask me if you can use it in that circumstance. By clearly stating the license is Creative Commons Attribution-Noncommercial-Share Alike, everyone knows that they can use it for whatever they want… but I do have some personal value for it.