Hello, Guest

By registering with us, you'll be able to discuss, share and private message with other members of our community.
What's new

Create synchronized NPCs.

Alexalsaud

Administrative
Staff member
Admin
Joined
Aug 5, 2024
Messages
337
ICoins
1,501

Create synchronized NPCs.


Create an npc on the server side (I took a clean server, created a server folder in it and created a index.js in this folder):
1725036304377.png

JavaScript:
mp.events.addCommand({
    "spawnPed": (player) => {
        if(player.myPed)player.myPed.destroy();//если уже есть удаляем и пересоздаем
        player.myPed = mp.peds.new(mp.joaat('csb_burgerdrug'), new mp.Vector3(player.position.x+1, player.position.y, player.position.z), {
            dynamic: true,//синхранизирован false не синхранизирован.
            invincible: false,//true - npс бесмертный false - npс смертный
        })
        player.myPed.controller = player;//указываем, кто будет выступать в роли контроллера.
        if(player.myVeh)player.myPed.data.veh = player.myVeh; //привязали к NPC машину чтобы в дальнейшем могли обратится именно к ней уже на клиенте
    }
});

Now, by writing /spawnPed in the chat, we can spawn our NPC
Why do we need a controller? I will only say that through the specified player, the NPC will be synchronized with other players.
You can change the controller at any time.

Let's move on. We need to give commands to our NPCs somehow. Let the NPC interact with the car, add the ability to spawn the car.
Augmenting our code on the server side.

JavaScript:
mp.events.addCommand({
    "spawnPed": (player) => {
        if(player.myPed)player.myPed.destroy();//если уже есть удаляем и пересоздаем
        player.myPed = mp.peds.new(mp.joaat('csb_burgerdrug'), new mp.Vector3(player.position.x+1, player.position.y, player.position.z), {
            dynamic: true,
            invincible: false,//true - npс бесмертный false - npс смертный
        })
        player.myPed.controller = player;//указываем, кто будет выступать в роли контроллера.
        if(player.myVeh)player.myPed.data.veh = player.myVeh; //привязали к NPC машину чтобы в дальнейшем могли обратится именно к ней уже на клиенте
    },
    "spawnVeh": (player, name) => {
        if(player.myVeh)player.myVeh.destroy();//если уже есть удаляем и пересоздаем
        player.myVeh = mp.vehicles.new(mp.joaat(name), new mp.Vector3(player.position.x+3, player.position.y, player.position.z), {
             numberPlate: 'RAGEMP',
             color:[[0,0,0],[0,0,0]],
             alpha: 255,
             locked: false,
             dimension: player.dimension,
         });
        if(player.myPed)player.myPed.data.veh = player.myVeh; //привязали к NPC машину чтобы в дальнейшем могли обратится именно к ней уже на клиенте
    }
});

Now we can create a car /spawnVeh Hash (/spawnVeh taxi)
The main question is, how to make the NPC perform some actions? Let's try to get our ward to get into the car. And reach us!
Everything is also on the server

JavaScript:
mp.events.addCommand({
    "spawnPed": (player) => {
        if(player.myPed)player.myPed.destroy();//если уже есть удаляем и пересоздаем
        player.myPed = mp.peds.new(mp.joaat('csb_burgerdrug'), new mp.Vector3(player.position.x+1, player.position.y, player.position.z), {
            dynamic: true,
            invincible: false,//true - npс бесмертный false - npс смертный
        })
        player.myPed.controller = player;//указываем, кто будет выступать в роли контроллера.
        if(player.myVeh)player.myPed.data.veh = player.myVeh; //привязали к NPC машину чтобы в дальнейшем могли обратится именно к ней уже на клиенте
    },
    "spawnVeh": (player, name) => {
        if(player.myVeh)player.myVeh.destroy();//если уже есть удаляем и пересоздаем
        player.myVeh = mp.vehicles.new(mp.joaat(name), new mp.Vector3(player.position.x+3, player.position.y, player.position.z), {
             numberPlate: 'RAGEMP',
             color:[[0,0,0],[0,0,0]],
             alpha: 255,
             locked: false,
             dimension: player.dimension,
         });
        if(player.myPed)player.myPed.data.veh = player.myVeh; //привязали к NPC машину чтобы в дальнейшем могли обратится именно к ней уже на клиенте
    },
    "ped": (player, command) => {
        player.myPed.data.command = command; //Даем команду NPС
    }
});

What's going on here? When we call the /ped command, we pass some command to the bot. (you don't have to do it this way, it's one of the ways)
That's not all. On the client, create a file index.js

1725036391433.png

And in this file, we write the following:

JavaScript:
switch (value) {
                case 'enterVeh':
                    let veh = entity.getVariable("veh");
                    if (veh){
                        entity.taskEnterVehicle(veh.handle, 10000, -1, 1, 1, 0);//даем задачу сесть в машину
                        entity.vehicle = veh;
                    }
                    else mp.gui.chat.push("ped: У меня нет машины в которую я мог бы сесть :(");
                    break;
                case 'goMe':
                    if (entity.vehicle) {
                        entity.taskVehiclePark(entity.vehicle.handle, player.position.x, player.position.y, player.position.z, 0, 0, 50, false); //ехать к персонажу
                    } else {
                        entity.taskGoToCoordAnyMeans(player.position.x, player.position.y, player.position.z, 6, 0, false, 1, 0); //идти к персонажу
                    }
                    break;
                default:
                    mp.gui.chat.push("ped: Я не знаю команды " + value);
                    break;
            }
        }
    })
})

All! Now we can control our puppet.
/ped enterVeh - will force the NPC to get into the stained car.
/ped goMe will force an NPC to come to you if they are not in the car, or force them to drive the car they got into.
All actions will be synchronized with other players!

DOWNLOAD
 
Back
Top