Posting this for reference, because I did not see anyone else do it yet.
If you already have a primary/secondary item, it overwrites whatever was in that slot, with the new weapon specified. So be careful.
Code:
new String:g_CS_WClasses[28][13] = {
"usp", //USP
"glock", //Glock
"deagle", //Desert Eagle (deagle)
"p228", //228 Compact
"elite", //Dualies (elite)
"fiveseven", //Five-Seven (fiveseven)
"m4a1", //Maverick M4A1 Colt (m4a1)
"ak47", //AK-47/CV-47 (ak47)
"aug", //Bullpup (aug)
"sg552", //Krieg 552 (sg552)
"galil", //Defender (galil)
"famas", //Clarion (famas)
"scout", //Scout (scout)
"sg550", //Krieg Commando (sg550)
"m249", //M249 (m249)
"g3sg1", //D3/AU1 (g3sg1)
"ump45", //UMP (ump45)
"mp5navy", //MP5 (mp5navy)
"m3", //Pump Shotgun (m3)
"xm1014", //Auto Shotgun (xm1014)
"tmp", //TMP (tmp)
"mac10", //Mac-10 (mac10)
"p90", //P90 (p90)
"awp", //AWP (awp)
"smokegrenade", //Smoke Grenade (smokegrenade)
"hegrenade", //HE Grenade (hegrenade)
"flashbang", //Flashbang Granade (flashbang)
"c4" //rofl? dont know if this is the real classname
};
public Action:_giveweapon(id,args)
{
if (args < 1)
{
ReplyToCommand(id, "[SM] Usage: sm_giveweapon <weaponid>");
return Plugin_Handled;
}
new String:wid_str[32];
GetCmdArg(1,wid_str,31);
new wid = StringToInt(wid_str);
if(wid > 27)
{
PrintToConsole(id,"[SM] WARNING: Don't be a newb. Weapon Indexes range from 0-27.");
return Plugin_Handled;
}
new String:WClass[32] = "weapon_";
StrCat(WClass,31,g_CS_WClasses[wid]);
GivePlayerItem(id,WClass);
return Plugin_Handled;
}
public Action:OnPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new id = GetClientOfUserId(GetEventInt(event, "userid"));
new Team = GetClientTeam(id);
if(Team == CS_TEAM_SPECTATOR || Team == CS_TEAM_NONE)
return Plugin_Continue;
if(!IsPlayerAlive(id))
return Plugin_Continue;
for(new i=0;i < 28;i++)
{
RemoveWeaponFromSlot(id, i);
}
return Plugin_Continue;
}
public bool:RemoveWeaponFromSlot(client, slot)
{
//get the entity id of the weapon in the client's slot specified
new bool:isRemoved;
new weapon_entid = GetPlayerWeaponSlot(client,slot);
//attempt to retrieve the weapon's entity id from the slot..
if(weapon_entid >= 0)
{
//get the classname of the weapon
new String:weapon_classname[32];
GetEdictClassname(weapon_entid, weapon_classname, 32);
if(strcmp(weapon_classname,"weapon_knife") == 0) return true;
//attempt to remove the weapon
isRemoved = RemovePlayerItem(client,weapon_entid);
RemoveEdict(weapon_entid);
if(isRemoved)
{
PrintToConsole(client,"Removed %s (entid:%d) from slot %d",weapon_classname,weapon_entid,slot);
}
else
{
PrintToConsole(client,"Could NOT remove %s (entid:%d) from slot %d",weapon_classname,weapon_entid,slot);
}
}
else
{
isRemoved = false;
//PrintToConsole(client,"No weapon to remove from slot %d",slot);
}
return isRemoved;
}
The "RemoveWeaponFromSlot()" function was taken from somewhere else on this forum, I would post a link to its source, but I could not find it. So just letting you know, it was not my function.
Tested and fully functional. C4 is a little weird tho, if you are CT, it gives it to you, and drops it.