Allowing 1 primary and 1 secondary in gamemode

Okay so I have a gamemode with guns all over the place but I only want people to be able to get one of each type, I don’t know why this isn’t working…



local primary = {
	"weapon_ak47",
	"weapon_m4a1",
	"weapon_mp5",
	"weapon_m249",
	"weapon_m3",
	"weapon_xm1014",
	"weapon_mac10",
	"weapon_awp",
	"weapon_scout",
	"weapon_tmp"
}

local secondary = {
	"weapon_deagle",
	"weapon_fiveseven",
	"weapon_glock",
	"weapon_usp"
}

function GM:PlayerCanPickupWeapon(client, weapon)
	
	local function IsSecondary(wep)
		for k,v in pairs(secondary) do
			if wep:GetClass() == v then
				return true
			end
		end
	end
	
	local function HasSecondary(ply)
		for k,wep in pairs(ply:GetWeapons()) do 
			if wep.Slot == 1 then
				return true
			end
		end
	end

	local function IsPrimary(wep)
		for k,v in pairs(primary) do
			if wep:GetClass() == v then
				return true
			end
		end
	end
	
	local function HasPrimary(ply)
		for k,wep in pairs(ply:GetWeapons()) do 
			if wep.Slot == 0 then
				return true
			end
		end
	end
	
	if IsPrimary(weapon) and HasPrimary(client) then
		return false
	end

	if IsSecondary(weapon) and HasSecondary(client) then
		return false
	end
	return true
end





local primary = {
	"weapon_ak47",
	"weapon_m4a1",
	"weapon_mp5",
	"weapon_m249",
	"weapon_m3",
	"weapon_xm1014",
	"weapon_mac10",
	"weapon_awp",
	"weapon_scout",
	"weapon_tmp"
}

local secondary = {
	"weapon_deagle",
	"weapon_fiveseven",
	"weapon_glock",
	"weapon_usp"
}

local function HasPrimary( ply )
	for k, wep in pairs( ply:GetWeapons() ) do 
		if ( table.HasValue( primary, wep:GetClass() ) ) then return true end
	end
end

local function HasSecondary( ply )
	for k, wep in pairs( ply:GetWeapons() ) do 
		if ( table.HasValue( secondary, wep:GetClass() ) ) then return true end
	end
end

function GM:PlayerCanPickupWeapon( client, weapon )
	if table.HasValue( primary, weapon:GetClass() ) and HasPrimary( client ) then
		return false
	end

	if table.HasValue( secondary, weapon:GetClass() ) and HasSecondary( client ) then
		return false
	end
	return true
end

Untested, but should work.

[editline]3rd June 2014[/editline]

Make sure you got the weapon classes correct.

The script you posted would prevent a player from picking up a gun they already have, which can be done simply with



	if ( client:HasWeapon(class) ) then
		return false
	end


But I am wanting them to only have one primary and one secondary. Sorry for the confusion…

It does do that, you add your desired primary/secondary weapons to the specified tables.

Yes, and I think I just realized that I was dumb and his code should work, but somehow it doesn’t. This is also for jailbreak, and I’ll go ahead and post the full code because it might be interfering or something…



local primary = {
	"weapon_ak47",
	"weapon_m4a1",
	"weapon_mp5",
	"weapon_m249",
	"weapon_m3",
	"weapon_xm1014",
	"weapon_mac10",
	"weapon_awp",
	"weapon_scout",
	"weapon_tmp"
}

local secondary = {
	"weapon_deagle",
	"weapon_fiveseven",
	"weapon_glock",
	"weapon_usp"
}

local function HasPrimary( ply )
	for k, wep in pairs( ply:GetWeapons() ) do 
		if ( table.HasValue( primary, wep:GetClass() ) ) then return true end
	end
end

local function HasSecondary( ply )
	for k, wep in pairs( ply:GetWeapons() ) do 
		if ( table.HasValue( secondary, wep:GetClass() ) ) then return true end
	end
end

function GM:PlayerCanPickupWeapon(client, weapon)
	if (self:PlayerUse(client, weapon) == false) then
		return false;
	end;

	local class = weapon:GetClass();

	if (class == "weapon_fists") then
		return true;
	end;

	local activeWeapon = client:GetActiveWeapon();

	if ( IsValid(activeWeapon) and activeWeapon:GetClass() == weapon:GetClass() ) then
		local ammoType = activeWeapon:GetPrimaryAmmoType();
		local ammoCount = client:GetAmmoCount(ammoType);

		if (ammoCount <= 0) then
			return true;
		end;
	end;

	if (IsValid(weapon.RealOwner) and client == weapon.RealOwner) then
		if ( weapon.OwnerPickup and weapon.OwnerPickup >= CurTime() ) then
			return false;
		end;
	end;

	if ( !client:HasWeapon(class) ) then
		return true;
	end;
	
	if table.HasValue( primary, weapon:GetClass() ) and HasPrimary( client ) then
		return false
	end

	if table.HasValue( secondary, weapon:GetClass() ) and HasSecondary( client ) then
		return false
	end

	return false;
end;


Maybe eliminate the table and make it purely based on Slot Position?



if ( !client:HasWeapon(class) ) then
		return true;
	end;

You do realize what this code does?

It allows you to pickup any weapon you don’t have, so anything below it is pretty much obsolete.

Alright, I felt dumb for not noticing that. Fixed everything, Thanks.