mp.events.add('playerLeaveVehicle', vehicle => {
if (vehicle === null || vehicle === undefined) return;
for (let doorIndex = 0; doorIndex <= 7; doorIndex++) {
const doorAngleRatio = vehicle.getDoorAngleRatio(doorIndex);
const doorOpen = doorAngleRatio > 0.1;
mp.events.callRemote('storeVehicleDoorState', vehicle, doorIndex, doorOpen);
mp.events.call('syncVehicleDoorState', vehicle.remoteId, doorIndex, doorOpen ? 1 : 0);
}
});
mp.events.add('entityStreamIn', entity => {
if (entity.type !== 'vehicle') return;
setTimeout(() => {
const doorStates = entity.getVariable('doorStates');
if (doorStates) {
for (let doorIndex in doorStates) {
const doorState = doorStates[doorIndex];
mp.events.call('syncVehicleDoorState', entity.remoteId, parseInt(doorIndex), doorState ? 1 : 0);
}
}
}, 1500);
});
mp.events.add('playerEnterVehicle', (vehicle, seat) => {
setTimeout(() => {
if (vehicle.getPedInSeat(-1) === mp.players.local.handle) {
const doorStates = vehicle.getVariable('doorStates');
if (doorStates) {
for (let doorIndex in doorStates) {
const doorState = doorStates[doorIndex];
mp.events.call('syncVehicleDoorState', vehicle.remoteId, parseInt(doorIndex), doorState ? 1 : 0);
}
}
}
}, 100);
});
mp.events.add('syncVehicleDoorState', (vehicleId, doorIndex, state) => {
const vehicle = mp.vehicles.atRemoteId(vehicleId);
if (vehicle && mp.vehicles.exists(vehicle)) {
if (state === 0) {
vehicle.setDoorShut(doorIndex, false);
} else if (state === 1) {
vehicle.setDoorOpen(doorIndex, false, true);
} else {
vehicle.setDoorBroken(doorIndex, true);
}
}
});