Log in to reply
 

Ped spawn error



  • I'm getting a NullReferenceException at "if (ped.Exists())" line in the following script:

        private Ped SpawnRandomPed(List<Ped> ListPeds, string[] ModelsList, string[] WeaponsList, int Accuracy, int Health, int BodyArmor, int RelationshipGroup)
        {
            string ModelName = RandomChoice<string>(ModelsList);
            string WeaponName = RandomChoice<string>(WeaponsList);
    
            PedHash pedSpawn;
            WeaponHash weaponGive;
    
            if (!Enum.TryParse<PedHash>(ModelName, true, out pedSpawn))
            {
                Wait(2000);
                throw new FormatException("Ped name " + ModelName + " does not exist!");
            }
    
            if (!Enum.TryParse<WeaponHash>(WeaponName, true, out weaponGive))
            {
                Wait(2000);
                throw new FormatException("Weapon name " + WeaponName + " does not exist!");
            }
    
    
            Vector3 PositionToUse;
            if (ListPeds == spawnedBodyguard)
            {
                PositionToUse = Game.Player.Character.GetOffsetInWorldCoords(new Vector3(-8, 0, 0));
            }
            else
            {
                if (Game.Player.Character.IsInVehicle())
                {
                    PedMinSpawnDistanceReal = PedMinSpawnDistance + 60;
                    PedMaxSpawnDistanceReal = PedMaxSpawnDistance + 60;
                }
                else
                {
                    PedMinSpawnDistanceReal = PedMinSpawnDistance;
                    PedMaxSpawnDistanceReal = PedMaxSpawnDistance;
                }
                RandomDistance = Function.Call<float>(Hash.GET_RANDOM_FLOAT_IN_RANGE, PedMinSpawnDistanceReal, PedMaxSpawnDistanceReal);
                PositionToUse = Game.Player.Character.Position.Around(PedMaxSpawnDistance);
            }
    
            Ped ped = World.CreatePed(pedSpawn, PositionToUse);
    
            WritePedDataToFile(ModelName, WeaponName, Accuracy, Health, BodyArmor, RelationshipGroup);
    
            if (ped.Exists())
            {
                if (ListPeds == spawnedBodyguard)
                {
                    Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, ped, relationshipGroupPlayer);
                    ped.RelationshipGroup = Game.Player.Character.RelationshipGroup;
                    ped.CanBeTargetted = true;
                    Function.Call(Hash.SET_PED_AS_GROUP_LEADER, Game.Player.Character, relationshipGroupPlayer);
                }
                else
                {
                    ped.RelationshipGroup = RelationshipGroup;
                }
            }
        }
    

    I don't know why this is happening. I even made a log (WritePedDataToFile) to see if there was anything wrong with the values but there is not.


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.