• How to make a custom team check to allow use of an entity.
    15 replies, posted
So a section of my code goes like this: function ENT:AcceptInput(input, activator, ply)     if input == "Use" then         net.Start("OpenMenu")         net.Send(ply)      end end My question is to only allow the following teams to be able to press E on the entity and open the menu: TEAM_REDSQUADRON ("Red Squadron Pilot") TEAM_BLUESQUADRON ("Blue Squadron Pilot") TEAM_GOLDSQUADRON ("Gold Squadron Pilot")
local allowedTeams = { TEAM_REDSQUADRON = true, TEAM_BLUESQUADRON = true, TEAM_GOLDSQUADRON = true, } function ENT:AcceptInput(inputType, activator, ply) if not allowedTeams[ply:Team()] then return end // <...> end
You can also use the ENTITY:Use hook for this instead of AcceptInput.
local allowedTeams = {   [TEAM_REDSQUADRON] = true,   [TEAM_BLUESQUADRON] = true,   [TEAM_GOLDSQUADRON] = true,   [TEAM_AIRMARSHAL] = true,   [TEAM_MOB] = true,   [TEAM_GRANDADMIRAL] = true,   [TEAM_HIGHGENERAL] = true,   [TEAM_SPECOPS] = true, } function ENT:Use(activator, ply)     if not allowedTeams[ply:Team()] then return end         net.Start("OpenMenu")         net.Send(ply)      end end So I am using this code, but it doesn't work, the entity just doesn't show up in the Q menu, and it is not throwing any error codes.
Your code is incorrect, try this : local allowedTeams = { TEAM_REDSQUADRON, TEAM_BLUESQUADRON, TEAM_GOLDSQUADRON, TEAM_AIRMARSHAL, TEAM_MOB, TEAM_GRANDADMIRAL, TEAM_HIGHGENERAL, TEAM_SPECOPS } function ENT:Use(activator, ply) if not table.HasValue( allowedTeams, ply:Team() ) then return end net.Start("OpenMenu") net.Send(ply) end
local allowedTeams = {   [TEAM_REDSQUADRON] = true,   [TEAM_BLUESQUADRON] = true,   [TEAM_GOLDSQUADRON] = true,   [TEAM_AIRMARSHAL] = true,   [TEAM_MOB] = true,   [TEAM_GRANDADMIRAL] = true,   [TEAM_HIGHGENERAL] = true,   [TEAM_SPECOPS] = true, } function ENT:Use(activator, ply)     if not allowedTeams[ply:Team()] then return end         net.Start("OpenMenu")         net.Send(ply)   end This is what I have right now, yet no one can open the menu for some reason. I press E on it, nothing happens.
What gamemode is this for? Where is this file located?
Its a SWRP server using DarkRP as a base, the file is located at /data/garrysmod/addons/darkrpmodification/lua/entities/testnpc
The team enums are most likely not available in time since they are loaded after. Try defining allowedTeams initially as an empty table then update it in an Initialize hook with the proper teams.
AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize()     self:SetModel("models/tfa/comm/gg/npc_cit_sw_droid_tactical.mdl")     self:SetHullType( HULL_HUMAN );     self:SetHullSizeNormal();     self:SetSolid( SOLID_BBOX )      self:SetMoveType( MOVETYPE_STEP )     self:CapabilitiesAdd(CAP_TURN_HEAD + CAP_ANIMATEDFACE)     self:SetHullType(HULL_HUMAN)     self:SetHullSizeNormal()     self:SetUseType( SIMPLE_USE )     self:SetMaxYawSpeed( 5000 )     self:SetSequence(ACT_IDLE) local allowedTeams = {   [TEAM_REDSQUADRON] = true,   [TEAM_BLUESQUADRON] = true,   [TEAM_GOLDSQUADRON] = true,   [TEAM_AIRMARSHAL] = true,   [TEAM_MOB] = true,   [TEAM_GRANDADMIRAL] = true,   [TEAM_HIGHGENERAL] = true,   [TEAM_SPECOPS] = true, } end util.AddNetworkString("OpenMenu") util.AddNetworkString("BuyDelta") util.AddNetworkString("BuyVWing") util.AddNetworkString("BuyYWing") util.AddNetworkString("BuyXWing") util.AddNetworkString("BuyArc") local allowedTeams = { } function ENT:Use(activator, ply)     if not allowedTeams[ply:Team()] then return end         net.Start("OpenMenu")         net.Send(ply)   end Like this? Im not sure what you mean when you say it needs to be initialized.
ENT:Initialize is different, you should use GM:Initialize. Ex. local allowedTeams = {} hook.Add("Initialize", "SetupUseTeams", function() allowedTeams = { [TEAM_FOO] = true, -- Insert rest.. } end) function ENT:Use(activator, ply) if not allowedTeams[ply:Team()] then return end net.Start("OpenMenu") net.Send(ply) end
Isn't the last Key/Value NOT supposed to have a trailing comma at the end before the end of the table in allowedTeams ?
It doesn't matter in Lua.
AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize()     self:SetModel("models/tfa/comm/gg/npc_cit_sw_droid_tactical.mdl")     self:SetHullType( HULL_HUMAN );     self:SetHullSizeNormal();     self:SetSolid( SOLID_BBOX )      self:SetMoveType( MOVETYPE_STEP )     self:CapabilitiesAdd(CAP_TURN_HEAD + CAP_ANIMATEDFACE)     self:SetHullType(HULL_HUMAN)     self:SetHullSizeNormal()     self:SetUseType( SIMPLE_USE )     self:SetMaxYawSpeed( 5000 )     self:SetSequence(ACT_IDLE) end util.AddNetworkString("OpenMenu") util.AddNetworkString("BuyDelta") util.AddNetworkString("BuyVWing") util.AddNetworkString("BuyYWing") util.AddNetworkString("BuyXWing") util.AddNetworkString("BuyArc") local allowedTeams = {} hook.Add("Initialize", "SetupUseTeams", function()   allowedTeams = {   [TEAM_REDSQUADRON] = true,   [TEAM_BLUESQUADRON] = true,   [TEAM_GOLDSQUADRON] = true,   [TEAM_AIRMARSHAL] = true,   [TEAM_MOB] = true,   [TEAM_GRANDADMIRAL] = true,   [TEAM_HIGHGENERAL] = true,   [TEAM_SO] = true,   } end) function ENT:Use(activator, ply)     if not allowedTeams[ply:Team()] then return end         net.Start("OpenMenu")         net.Send(ply)   end Here is my current code, when I press E on the entity as any job it still does not work.
Put a print in the Use function to check if it's being ran.
Good idea, it takes a while to check however as the server I'm putting this in has people on it so I can't restart every few minutes
Sorry, you need to Log In to post a reply to this thread.