Weapon slots
#1

Tried coding this into my script to restrict the amount of player weapon slots but it doesn't work.

pawn Код:
public OnGameModeInit()
{
    SetTimerEx("WeaponSlotsForPlayer", 1000, true, "d", 1337);
        return 1;
}

forward IsValidWeaponID(weaponid);
public IsValidWeaponID(weaponid)
{
    if((weaponid > 0 && weaponid < 19) || (weaponid > 21 && weaponid < 47))
    return 1;

    return 0;
}

forward WeaponSlotsForPlayer(playerid, slotid1, slotid2, slotid3);
public WeaponSlotsForPlayer(playerid, slotid1, slotid2, slotid3)
{
    new Weaponid[13], ammo[13];
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, Weaponid[i], ammo[i]);

        if(IsValidWeaponID(Weaponid[i]) == 1 && (i != slotid1 && i != slotid2 && i != slotid3))
        {
            GivePlayerWeapon(playerid, Weaponid[i], -ammo[i]);
        }
        SendClientMessage(playerid, COLOR_LIGHTBLUE, "INFO: You are not allowed to carry more than 3 weapons");
    }
    return 1;
}
Reply
#2

it's wrong, You're getting their weapons, comparing if it's valid and giving it back, with less ammo. And, you're calling a settimerex of a wrong way.

you should, loop their weapons, compare if it's more than the allowed, and then remove it instead of give it back. You can do it, by reseting their weapons.
Reply
#3

Well, I gave you this code few days ago and I understand what you've done wrong. First of all, you shouldn't be using SetTimerEx (Regardless of your wrong use of it), you should use SetTimer instead:

pawn Код:
SetTimer("CheckPlayerSlots", 1000, true);
This would call a timer as soon as the gamemode starts which will call the forwarded public CheckPlayerSlots. So, let's forward the public:

pawn Код:
forward CheckPlayerSlots()
// Forwared. Now let's code it
public CheckPlayerSlots()
{
    for(new p = 0; p < MAX_PLAYERS; p ++) // loop through all players
    {
        if(p == INVALID_PLAYER_ID || IsPlayerNPC(p))
            continue; // skip if the player is a npc or not connected

        // Here use your function WeaponSlotsForPlayer
        WeaponSlotsForPlayer(p, 1, 2, 3); // Just an example. Change the slots ids!
    }
    return 1;
}
Ah and there's something you should modify in your WeaponSlotsForPlayer function (Read leonardo1434's reply about the weapons). So, instead of

pawn Код:
GivePlayerWeapon(playerid, Weaponid[i], -ammo[i]);
Use:
pawn Код:
RemovePlayerWeapon(playerid, Weaponid[i]);
Ofcourse this is an alternative function, so here's the defination of it:
pawn Код:
stock RemovePlayerWeapon(playerid, weaponid)
{
    new pWeaps[13], pAmmo[13];

    for(new i = 0; i < 13; i ++)
    {
        new weap, ammo;

        GetPlayerWeaponData(playerid, i, weap, ammo);
        if(weap != weaponid)
        {
            GetPlayerWeaponData(playerid, i, pWeaps[i], pAmmo[i]);
        }
    }
    ResetPlayerWeapons(playerid);
    for(new s = 0; s < 13; s ++)
    {
        GivePlayerWeapon(playerid, pWeaps[s], pAmmo[s]);
    }
    return 1;
}
Reply
#4

Well, I tried doing everything you guys told me here but it keeps spamming the chat with the sendclientmessage
Reply
#5

Quote:
Originally Posted by Nicholas.
Посмотреть сообщение
Well, I tried doing everything you guys told me here but it keeps spamming the chat with the sendclientmessage
Show what you made.
Reply
#6

pawn Код:
/* OnGameModeInit */
SetTimer("CheckPlayerSlots", 1000, true);

forward CheckPlayerSlots(playerid, slotid1, slotid2, slotid3);
public CheckPlayerSlots(playerid, slotid1, slotid2, slotid3)
{
    for(new p = 0; p < MAX_PLAYERS; p ++)
    {
        if(p == INVALID_PLAYER_ID || IsPlayerNPC(p))
        continue;

        WeaponSlotsForPlayer(p, slotid1, slotid2, slotid3);
    }
    return 1;
}

stock IsValidWeaponID(weaponid)
{
    if((weaponid > 0 && weaponid < 19) || (weaponid > 21 && weaponid < 47))
    return 1;

    return 0;
}

stock WeaponSlotsForPlayer(playerid, slotid1, slotid2, slotid3)
{
    new Weaponid[13], ammo[13];
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, Weaponid[i], ammo[i]);

        if(IsValidWeaponID(Weaponid[i]) == 1 && (i != slotid1 && i != slotid2 && i != slotid3))
        {
            RemovePlayerWeapon(playerid, Weaponid[i]);
        }
        SendClientMessage(playerid, COLOR_LIGHTBLUE, "INFO: You are not allowed to carry more than 3 weapons");
    }
    return 1;
}

stock RemovePlayerWeapon(playerid, weaponid)
{
    new pWeapon[13], pAmmo[13];

    for(new i = 0; i < 13; i ++)
    {
        new Weapon, Ammo;

        GetPlayerWeaponData(playerid, i, Weapon, Ammo);
        if(Weapon != weaponid)
        {
            GetPlayerWeaponData(playerid, i, pWeapon[i], pAmmo[i]);
        }
    }
    ResetPlayerWeapons(playerid);
    for(new s = 0; s < 13; s ++)
    {
        GivePlayerWeapon(playerid, pWeapon[s], pAmmo[s]);
    }
    return 1;
}
Reply
#7

Shouldn't the message be sent when the player has a disallowed weapon ?

I'm talking about this:

pawn Код:
for(new i = 0; i < 13; i++)
{
    GetPlayerWeaponData(playerid, i, Weaponid[i], ammo[i]);
    if(IsValidWeaponID(Weaponid[i]) == 1 && (i != slotid1 && i != slotid2 && i != slotid3))
    {
        RemovePlayerWeapon(playerid, Weaponid[i]);
    }
    SendClientMessage(playerid, COLOR_LIGHTBLUE, "INFO: You are not allowed to carry more than 3 weapons");
}
You're here looping through the 12 slots and sending the player a message anyway without checking if he has disallowed weapon (slot) or not, hence comes the problem, it will send the message to the player 13 times even if he doesn't have a disallowed weapon. Here's a more efficient way of doing it:

pawn Код:
stock WeaponSlotsForPlayer(playerid, slotid1, slotid2, slotid3)
{
    new Weaponid[13], ammo[13];
    new count = 0; // declare a variable to count how many disallowed weapons a player has
    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, Weaponid[i], ammo[i]);

        if(IsValidWeaponID(Weaponid[i]) == 1 && (i != slotid1 && i != slotid2 && i != slotid3))
        {
            RemovePlayerWeapon(playerid, Weaponid[i]);
            count ++; // adds 1 to that variable when it finds a disallowed weapon
        }
    }
    // Loop ended. Now, let's check if 'count' got any changes
    if(count != 0) // if it's not equal to 0 which means it's been changed
    {
        // Send your warning message
        SendClientMessage(playerid, COLOR_LIGHTBLUE, "INFO: You are not allowed to carry more than 3 weapons");
    }
    return 1;
}
Reply
#8

thats not going to work. Im going try a different method, thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)