View Single Post
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-22-2019 , 10:54   Re: [CSGO] Fetch client's weapons, grenades, etc...
Reply With Quote #14

I think that the problem is that you have the taser and the knife on the same slot and the GetPlayerWeaponSlot functions will only return the first weapon on that slot.That's why is only returning your first grenade only.
Try like this. This should store the first 3 weapons on that slot. Check to see what is the debug print output after you try this.
Code:
#include <sourcemod>
#include <sdktools>

#pragma newdecls required 
#define WEAPONS_MAX_LENGTH 32
#define WEAPONS_SLOTS_MAX 5
#define WEAPONS_ON_SLOTS_MAX 3

enum WeaponsSlot
{
    Slot_Invalid        = -1,   /** Invalid weapon (slot). */
    Slot_Primary        = 0,    /** Primary weapon slot. */
    Slot_Secondary      = 1,    /** Secondary weapon slot. */
    Slot_Melee          = 2,    /** Melee (knife) weapon slot. */
    Slot_Projectile     = 3,    /** Projectile (grenades, flashbangs, etc) weapon slot. */
    Slot_Explosive      = 4,    /** Explosive (c4) weapon slot. */
};
public void OnPluginStart()
{
	RegConsoleCmd("sm_test",TestCMD);
}
public Action TestCMD(int client,int args)
{
	bool hasWeapon = WeaponsClientHasWeapon(client,"weapon_healthshot");
	if(hasWeapon)
	{
		PrintToChatAll("Has healthshot");
	}
}
stock bool WeaponsClientHasWeapon(int client, const char weapon[32])
{
    // Get all of client's current weapons.
    int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX];
    WeaponsGetClientWeapons(client, weapons);  
    char classname[64];
    
    // x = slot index
    for (int x = 0; x < WEAPONS_SLOTS_MAX; x++)
    {
		for(int w = 0;w < WEAPONS_ON_SLOTS_MAX;w++) 
		{
			// If slot is empty, then stop.
			if (weapons[x][w] == -1) 
			{
				continue;
			}
			
			// If the weapon's classname matches, then return true.
			GetEdictClassname(weapons[x][w], classname, sizeof(classname));
			PrintToChat(client,"[DEBUG] User weapon %s , weapon to check for : %s",classname,weapon);
			
			if (StrEqual(weapon, classname, false))
			{
				return true;
			}
		}
    }   
    return false;
}
stock void WeaponsGetClientWeapons(int client, int weapons[WeaponsSlot][WEAPONS_ON_SLOTS_MAX])
{
    // x = Weapon slot.
	// Loop twice for the teaser and healthshot ( cuz are on the same slot as other weapons )
    for (int x = 0; x < WEAPONS_SLOTS_MAX; x++)
    {
		for(int w = 0;w < WEAPONS_ON_SLOTS_MAX;w++)
		{
		    // if there are more weapons this SHOULD save all of them from the slot if we loop more than once
			weapons[x][w] = GetPlayerWeaponSlot(client, x);
		}
    }
}

Last edited by ShD3luxe; 08-22-2019 at 10:56.
ShD3luxe is offline