Raised This Month: $51 Target: $400
 12% 

Solved Stripping everything but knife [CS:GO]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
FuZ!on
Senior Member
Join Date: Aug 2007
Old 01-23-2021 , 09:36   Stripping everything but knife [CS:GO]
Reply With Quote #1

I'm trying to remove all weapons on player spawn except for the knife. I'm struggling to understand why the below code is removing all weapons.

PHP Code:
public void Event_PlayerSpawn(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));

    if (
IsValidClient)
    {
        
int weapon = -1;
        for (new 
0<= 5i++)
        {
            if ((
weapon GetPlayerWeaponSlot(client,i)) != -1)
            {
                
char weaponName[33];
                
GetEntityClassname(weaponweaponNamesizeof(weaponName));
                
                if (
StrContains(weaponName"knife") == -|| StrContains(weaponName"bayonet") == -1)
                {                 
                    
RemovePlayerItem(clientweapon);
                    
AcceptEntityInput(weapon"Kill");
                }
            }
        }
    }

To my understanding, StrContains() will return -1 if the string is not found. I tested this by adding some PrintToChat() messages to print out the StrContains() values and confirmed that it's producing a -1 value for weapons that are not a knife but still goes ahead and strips everything.

Last edited by FuZ!on; 01-23-2021 at 10:06.
FuZ!on is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 01-23-2021 , 09:43   Re: Stripping everything but knife [CS:GO]
Reply With Quote #2

Because you used || instead of &&, so even if the string doesn't contain "knife", it will still continue if the string also doesn't contain "bayonet".
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
FuZ!on
Senior Member
Join Date: Aug 2007
Old 01-23-2021 , 10:05   Re: Stripping everything but knife [CS:GO]
Reply With Quote #3

Quote:
Originally Posted by DJ Tsunami View Post
Because you used || instead of &&, so even if the string doesn't contain "knife", it will still continue if the string also doesn't contain "bayonet".
You're right... I see my mistake now.

I was reading that statement in my head as if it were "If it doesn't contain knife or bayonet, do this" but failed to realize that it was treating it as two different statements as in "If it doesn't contain knife, do this or, if it doesn't contain bayonet, do this."
FuZ!on is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 01-23-2021 , 10:58   Re: Stripping everything but knife [CS:GO]
Reply With Quote #4

PHP Code:

void StripWeaponsAndGiveKnife
(int client)
{
    
int entity CreateEntityByName("game_player_equip");
    if (
entity == -1)
    {
        
LogError("Unable to create \"game_player_equip\" entity.");
        return;
    }

    
DispatchKeyValue(entity"weapon_knife""0");
    
DispatchKeyValue(entity"spawnflags""3");

    
DispatchSpawn(entity);
    
ActivateEntity(entity);
    
    
AcceptEntityInput(entity"Use"client);
    
RemoveEntity(entity);

__________________

Last edited by Ilusion9; 01-23-2021 at 11:47.
Ilusion9 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode