Weapon limiting (PlayerCanPickUpWeapon)

Does someone out there have a fully functional code(s) with the function, only allowing people to pick up 1 primary weapon and one secondary weapon (example: 1 AK47 and 1 Deagle) preventing them from picking up more guns, so they dont run around with 8 different weapons. I tried it myself, with no luck. I used these functions and hooks.

weapon:GetClass()
weapon:GetPrimaryAmmoType()
ply:HasWeapon()

function GM:PlayerCanPickupWeapon(ply, weapon)

Thanks in advance!

[lua]
local primary = {

"weapon_class",
"more_classes"

}

local sec = {

"weapon_class",
"more_classes"

}

local allowed = true
function GM:PlayerCanPickupWeapon( ply, wep )

local weps = ply:GetWeapons()

if table.HasValue( primary, wep:GetClass() ) then

    for _, x in pairs(primary) do

        if table.HasValue( weps, x:GetClass() ) then

            allowed = false

        end

    end

elseif table.HasValue( sec, wep:GetClass() ) then

    for _, x in pairs(sec) do

        if table.HasValue( weps, x:GetClass() ) then

           allowed = false

        end
        
    end

end

if allowed then

    return true

else

    return false

end

end
[/lua]

Might work, might not work. Cannot test at the moment, sorry.

If it doesn’t work, try this
http://forum.facepunch.com/showthread.php?t=1322132

From TTT:



-- Prevent players from picking up multiple weapons of the same type etc
function GM:PlayerCanPickupWeapon(ply, wep)
   if not IsValid(wep) and not IsValid(ply) then return end

   -- Disallow picking up for ammo
   if ply:HasWeapon(wep:GetClass()) then
      return false
   elseif not ply:CanCarryWeapon(wep) then
      return false
   elseif IsEquipment(wep) and wep.IsDropped and (not ply:KeyDown(IN_USE)) then
      return false
   end

   local tr = util.TraceEntity({start=wep:GetPos(), endpos=ply:GetShootPos(), mask=MASK_SOLID}, wep)
   if tr.Fraction == 1.0 or tr.Entity == ply then
      wep:SetPos(ply:GetShootPos())
   end

   return true
end


None of them have been working for me, i’ve been trying and trying and cant get it down, thanks for the feedback btw.

I tried some different things, for example i tried this

[LUA]if table.HasValue( SKY.PRIMARY, weapon:GetClass() ) then

			for _, x in pairs(SKY.PRIMARY) do
			
				if table.HasValue( ply:GetWeapons(), x:GetClass() ) then
				
						return false[/LUA]

SKY.PRIMARY, is the table I have with weapon that is listed by me as a primary weapon. But it calls an error



[ERROR] gamemodes/darkrp/gamemode/modules/base/sv_gamemode_functions.lua:491: bad key to string index (number expected, got string)
  1. error - [C]:-1
   2. __index - lua/includes/extensions/string.lua:262
    3. Call - gamemodes/darkrp/gamemode/modules/base/sv_gamemode_functions.lua:491
     4. unknown - gamemodes/darkrp/entities/entities/spawned_weapon/init.lua:53



I just cant see how the, weapon that is trying to be picked up, its crosschecked by the table i made. Please help me out, I know there is some smart person out there!

bump

what for?

Im having the same problem

I would encourage writing your own instead of just downloading or pasting someones code.
It’s useful to look at their code can give you the missing piece if you are confused sometimes.

You shouldn’t let other people think for you, you already have some of the correct hooks.


weapon:GetClass()
weapon:GetPrimaryAmmoType()
ply:HasWeapon()
function GM:PlayerCanPickupWeapon(ply, weapon)

You posted all of these, which means you know how to navigate the wiki and think logically.
GM:PlayerCanPickupWeapon Is your key, if you return true the player can pickup the gun, if you return false they cannot pickup the gun.

Your limits and restrictions are up to you.

You can use the weapon class name and compare it to an array of “primary” and “secondary” weapons to check if they already have one, and if so return false.
Otherwise return true.

You could do the same thing with ammo type instead, which could simplify the length of the comparison especially if you have lots of guns, and work for every gun using “default” or “standard” ammo types.

It’s up to you to decide what works for you, you get to build the whole system. There could be even better and more clever ways to handle your weapons depending on your specific needs, which is why it’s good practice to learn to fish for yourself.

Here Mick, this is something you can work off of:
[lua]
if SERVER then
local WEPS_Primary = {
weapon_smg1 = true,
weapon_shotgun = true
}

local WEPS_Secondary = {
	weapon_357 = true,
	weapon_pistol = true
}

hook.Add("PlayerCanPickupWeapon", "WEPS.PlayerCanPickupWeapon", function(ply, wep)
	local wepClass = wep:GetClass()
	if ply:HasWeapon(wepClass) then
		return false
	end

	local plyWeps = ply:GetWeapons()
	local isPrimary, isSecondary = WEPS_Primary[wepClass], WEPS_Secondary[wepClass]
	
	for k,v in ipairs(plyWeps) do
		local vClass = v:GetClass()
		local plyHasPrimary, plyHasSecondary = WEPS_Primary[vClass], WEPS_Secondary[vClass]
		if isPrimary and plyHasPrimary then
			return false
		elseif isSecondary and plyHasSecondary then
			return false
		end
	end
	
	return true
end)

end
[/lua]

Instead of defining the weapons that take up slots and hard-coding them. I’d recommend using the SWEP.Slot id to reference for whether or not it’s primary or secondary. That saves n number of lines per weapons you have.