• Any help would be appreciated :) Trying to blacklist certain weapons in script
    3 replies, posted
Hi there! So I have a script that I'm trying to improve upon. It's designed to allow people to manually pick up a weapon by pressing E on a weapon entity in the world. It has a weapon hold limit that can be specified in a convar, and if that limit is reached, it allows you to drop your currently equipped weapon to pick up the new one. For my server, I'm trying to make it so that certain weapon classes cannot be dropped to pick up a new weapon. Sounds fairly simple right? But whenever I try to add the blacklist, the entire script stops working. An example of 2 weapon classes that I want to blacklist are "weapon_mu_hands" and "nightvision". If anyone could please help point me in the right direction, I'd really appreciate it! Here is the working code so far that allows any weapon to drop to pick up a new one... I'm really sorry for the messiness of the code, it appears better spaced out in the actual lua file... CreateConVar("sv_manualweaponpickup", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Is manual weapon pickup enabled?") CreateConVar("sv_manualweaponpickup_aim", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Must the player be aiming at the weapon?") CreateConVar("sv_manualweaponpickup_auto", 0, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Holding use key picks up weapons automatically.") CreateConVar("sv_manualweaponpickup_autodraw", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Player will automatically draw a given weapon.") CreateConVar("sv_manualweaponpickup_weaponlimit", 0, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "How many weapons a player can hold at once.  (0 = No Limit)") CreateConVar("sv_manualweaponpickup_weaponlimitswap", 1, {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Drop current weapon to pick up another if player is holding too many.") local plmeta = FindMetaTable("Player") function plmeta:Give(classname) local ent = ents.Create(classname) if (!IsValid(ent)) then return end ent:SetPos(self:GetPos()) ent.GiveTo = self ent:Spawn() if (GetConVar("sv_manualweaponpickup_autodraw"):GetBool()) then timer.Simple(0.1, function() self:SelectWeapon(classname) end) end end plmeta._DropWeapon = plmeta.DropWeapon function plmeta:DropWeapon(weapon) if (IsValid(weapon)) then self:_DropWeapon(weapon) weapon.GiveTo = nil end end local function canCarryWeapon(pl, weapon) local limit = GetConVar("sv_manualweaponpickup_weaponlimit"):GetInt() if (limit != 0 && #pl:GetWeapons() >= limit) then if (GetConVar("sv_manualweaponpickup_weaponlimitswap"):GetBool()) then if (pl.PressedUse) then pl.PressedUse = false pl:DropWeapon(pl:GetActiveWeapon()) pl.DrawWeapon = weapon:GetClass() timer.Simple(0.1, function() pl:SelectWeapon(pl.DrawWeapon) end) return true end return false end return true end hook.Add("PlayerCanPickupWeapon", "ManualWeaponPickup_CanPickup", function(pl, ent) if (pl.ManualWeaponPickupSpawn) then if (CurTime() > pl.ManualWeaponPickupSpawn) then if (IsValid(ent.GiveTo)) then if (ent.GiveTo == pl) then return true end end if (GetConVar("sv_manualweaponpickup"):GetBool()) then if (pl:KeyDown(IN_USE)) then if (GetConVar("sv_manualweaponpickup_aim"):GetBool()) then if (pl:GetEyeTrace().Entity == ent) then if (!GetConVar("sv_manualweaponpickup_auto"):GetBool()) then if (pl.PressedUse) then local c = canCarryWeapon(pl, ent) pl.PressedUse = false return c else return false end else return canCarryWeapon(pl, ent) end else return false end else if (!GetConVar("sv_manualweaponpickup_auto"):GetBool()) then if (pl.PressedUse) then pl.PressedUse = false return canCarryWeapon(pl, ent) else return false end else return canCarryWeapon(pl, ent) end end else return false end end end end end) hook.Add("KeyPress", "ManualWeaponPickup_KeyPress", function(pl, key) if (key == IN_USE) then pl.PressedUse = true end end) hook.Add("PlayerSpawn", "ManualWeaponPickup_PlayerSpawn", function(pl) pl.ManualWeaponPickupSpawn = CurTime() end) concommand.Add("dropweapon", function(pl) pl:DropWeapon(pl:GetActiveWeapon()) end)
If someone could help with this I'd really appreciate it because I'm completely stuck... It seems making separate scripts to prevent weapon drops would require multiple hooks, I'm unable to just use a SWEP.AllowDrop = false command on the weapons in question because of how the manual weapon pickup code works... I can't even cause Pointshop purchased weapons to become a part of the weapon limit in this code. It breaks everything because it's horribly incompatible... I've been researching this for 3 days and I keep coming up completely empty on this specific problem.... which shocks me, because I really thought that creating a blacklist to prevent certain weapon classes from being dropped would be a simple matter. I know coding can get very abstract, but THIS? Seriously, I'll take anything I can get. Blacklists are something completely new to me, as I've really only experience in coding OnDeath functions, mat textures changes, and OnModify functions.
local blacklist = { ["something_a"] = true, ["something_b"] = true } function IsSomethingBlacklisted(something) return blacklist[something] == true end IsSomethingBlacklisted("something_a") -- output : true IsSomethingBlackliste("nothing") -- output : false
Thank you for getting back to me. I've turned the code into this then so far: local function canCarryWeapon(pl, weapon) local limit = GetConVar("sv_manualweaponpickup_weaponlimit"):GetInt() if (limit != 0 && #pl:GetWeapons() >= limit) then        if (GetConVar("sv_manualweaponpickup_weaponlimitswap"):GetBool()) then                          local blacklist = {                 ["weapon_mu_hands"] = true,                 ["nightvision"] = true, ["weapon_ciga"] = true, ["dab"] = true, ["facepunch"] = true, ["flip"] = true, ["frontflip"] = true, ["weapon_cleaner"] = true, ["weapon_spray"] = true, ["middlefinger"] = true, ["salute"] = true, ["selfportrait_camera"] = true, ["surrender"] = true             }                          function IsSomethingBlacklisted(something)             return             blacklist[something] == true             end                          if (pl.PressedUse) then                 pl.PressedUse = false                 if (IsSomethingBlacklisted(pl:GetActiveWeapon()) == true) then                     return true                 else                     pl:DropWeapon(pl:GetActiveWeapon())                     pl.DrawWeapon = weapon:GetClass()                     timer.Simple(0.1, function() pl:SelectWeapon(pl.DrawWeapon) end)                     return true         end         return false end return true end But there is still a problem where the addon doesn't get detected anymore since the convars are no longer recognized in the console.
Sorry, you need to Log In to post a reply to this thread.