IndexOutOfRangeException in hook OnPlayerWound
Why i getting a error Failed to call hook 'OnPlayerWound' on plugin 'MedkitSaver v1.0.0' (IndexOutOfRangeException: Index was outside the bounds of the array.)
My code:
        object OnPlayerWound(BasePlayer player, BasePlayer source)
        {          
            int calls = 0;
            Item[] Items = new Item[] {};
            Items = player?.inventory?.AllItems();
            foreach(Item playerItems in Items)
            {
                calls += 1;
                if(calls > 40) break;
                if(Items[calls].ToString().Contains("Item.largemedkitx1"))
                {
                    CuiHelper.AddUi(player, GUI);
                    steamIDs = steamIDs + " ," + player?.IPlayer?.Id?.ToString();
                    break;
                }
            }
            return null;
        }​

You are probably trying to reference an element in the array using an index that is larger than the amount of elements in the array.

So basically say there are 3 things in the array, if you do myArray[3], it will throw an error since there is no element at index 3.

You can do this or you can also check inventory for amount of an item.

object OnPlayerWound(BasePlayer player, BasePlayer source)
{
   Item[]  Items = player?.inventory?.AllItems();
if (items == null) return null;
    foreach(Item playerItems in Items)
    {
        if(playerItems.name.Contains("largemedkit"))
        {
            CuiHelper.AddUi(player, GUI);
            steamIDs = steamIDs + " ," + player?.IPlayer?.Id?.ToString();
            break;
        }
    }
    return null;
}


Merged post

To simple that down for you..

void OnPlayerWound(BasePlayer player, BasePlayer source)
{
    if(player.inventory.GetAmount(254522515) >= 1)
    {
        CuiHelper.AddUi(player, GUI);
        steamIDs = steamIDs + " ," + player?.IPlayer?.Id?.ToString();
    }
}​
5f542b9181eac.png 0x89A

You are probably trying to reference an element in the array using an index that is larger than the amount of elements in the array.

So basically say there are 3 things in the array, if you do myArray[3], it will throw an error since there is no element at index 3.

thx for the clarification

Ts3hosting

You can do this or you can also check inventory for amount of an item.

object OnPlayerWound(BasePlayer player, BasePlayer source)
{
   Item[]  Items = player?.inventory?.AllItems();
if (items == null) return null;
    foreach(Item playerItems in Items)
    {
        if(playerItems.name.Contains("largemedkit"))
        {
            CuiHelper.AddUi(player, GUI);
            steamIDs = steamIDs + " ," + player?.IPlayer?.Id?.ToString();
            break;
        }
    }
    return null;
}


Merged post

To simple that down for you..

void OnPlayerWound(BasePlayer player, BasePlayer source)
{
    if(player.inventory.GetAmount(254522515) >= 1)
    {
        CuiHelper.AddUi(player, GUI);
        steamIDs = steamIDs + " ," + player?.IPlayer?.Id?.ToString();
    }
}​

thx for the code example)