Author Topic: Is this how to properly restrict the spawn and context menus to certain ranks?  (Read 205 times)

0 Members and 1 Guest are viewing this topic.

Offline VORTEXGaming

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Code: [Select]
if SANDBOX:SpawnMenuOpen() then
function checkULX(ply)
    if table.HasValue({"owner", "dev", "superadmin"}, ply:GetNWString("usergroup")) then
        return true
else return false
    end
end

if GM:ContextMenuOpen() then
function checkULX(ply)
    if table.HasValue({"owner", "dev", "superadmin"}, ply:GetNWString("usergroup")) then
        return true
else return false
    end
end

Offline BlueNova

  • Newbie
  • *
  • Posts: 29
  • Karma: 5
    • View Profile
Do you mean the auto pop up motd menu?

Offline VORTEXGaming

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Look at the title

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7553
  • Karma: 337
  • Sertafide Ulysses Jenius
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
In simplest answer, no, that is not correct.
SANDBOX:SpawnMenuOpen and GM:ContextMenuOpen are HOOKS.
You monitor for hooks using hook.Add

Add hook looks for the events you want, and then calls a function passing some information, usually the player that caused the event.
You could write functions to then check the player's group and return false if they were not a member of your groups you want.

You'd also likely want to check, the way you're currently checking, using ULib's GetUserGroup.
I'm not sure your GetNW string would work consistently or at all.
If you don't use ULib, unfortunately, GMod's closest check is Player:IsUserGroup. With that, you'd have to do a for loop through your table checking if player was part of the table.
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin

Offline Alpha Universe

  • Newbie
  • *
  • Posts: 11
  • Karma: 0
    • View Profile
@JamminR What is the correct Code then?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 472
  • Karma: 83
  • I write code
    • View Profile
You'd also likely want to check, the way you're currently checking, using ULib's GetUserGroup.
I'm not sure your GetNW string would work consistently or at all.
If you don't use ULib, unfortunately, GMod's closest check is Player:IsUserGroup. With that, you'd have to do a for loop through your table checking if player was part of the table.

Garry's Mod actually has Player:GetUserGroup by default, and has had it for some time actually (I wonder if I could have used more conjugations of 'have,' or more 'actually's). I just found this out after I recently went spelunking for such a function in vanilla Garry's Mod because I was too lazy to install ULib. I don't know if networked strings are case-sensitive, but the default implementation of Player:GetUserGroup is almost identical to what VORTEXGaming is doing except for the casing and the fallback:

Code: Lua
  1. function meta:GetUserGroup()
  2.     return self:GetNWString("UserGroup", "user")
  3. end

@JamminR What is the correct Code then?

Take a look at the documentation for hook.Add() that JamminR has so kindly linked to. It includes some examples that show you how to run code using hooks.

Let me know if you need more hints.
« Last Edit: January 02, 2017, 01:23:44 PM by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.