Flow Posted March 12, 2018 Share Posted March 12, 2018 Hey, does anybody have a working example how to interact with vehicles or other objects like peds when you are close to them? I tried the following which won't return the id of police cars or peds but also that seems not to work: // some keybind before so this is executed when you press L let closestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70); mp.game.graphics.notify("Closest Vehicle ID: " + closestVehicle); which will give an id of the vehicle. But if i try to use that vehicle with another action it will not work. For simple test i tried to explode the vehicle with the following command let vehicleAct = mp.vehicles.at(closestVehicle) vehicleAct.explode(); How do i interact with the given id as this seems not to work? I need a solution in Javascript only. Thanks and regards Link to comment Share on other sites More sharing options...
misco Posted March 12, 2018 Share Posted March 12, 2018 Might be worth trying to use let vehicleAct = mp.vehicles.atHandle(closestVehicle); These native functions return object handle id more likely than id in pool, also ive noticed that handle id changes when entity streams out and in. 2 Link to comment Share on other sites More sharing options...
Flow Posted March 12, 2018 Author Share Posted March 12, 2018 let thenearestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70); mp.game.graphics.notify("veh id: " + thenearestVehicle); let testob = mp.vehicles.atHandle(thenearestVehicle); testob.explode(); Nope. Neither works. Link to comment Share on other sites More sharing options...
Captien Posted March 12, 2018 Share Posted March 12, 2018 Just Explode it directly, No need to get its handle. mp.game.vehicle.getClosestVehicle gets the vehicle object direct, so just explode it directly Link to comment Share on other sites More sharing options...
misco Posted March 12, 2018 Share Posted March 12, 2018 (edited) No, did some quick test getClosestVehicle returned for me typeof(thenearestVehicle) is number and JSON.stringify(thenearestVehicle) is 7682 which i consider to be handle id. When you add some more test code from wiki for example testob.setTyreBurst(0, false, 1000); testob.setTyreBurst(1, false, 1000); testob.setTyreBurst(4, false, 1000); testob.setTyreBurst(5, false, 1000); you will see that the closest vehicle has all tires popped but did not explode, because .explode() method does not seem to be working even trying example from wiki if( commandName === "exptest") { var vehicle = mp.vehicles.new(989294410, localPlayer.position); //Spawns Voltic2 at 'player' position vehicle.explode(); } car was spawned without explosion in current 0.3.5 release. So stuff with atHandle mentioned earlier is the right way to do this edit: btw i made it explode using testob.explodeInCutscene(true); Edited March 12, 2018 by misco made it explode with other function 1 Link to comment Share on other sites More sharing options...
Bryan63 Posted March 12, 2018 Share Posted March 12, 2018 4 часа назад, cmdflow сказал: let thenearestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70); mp.game.graphics.notify("veh id: " + thenearestVehicle); let testob = mp.vehicles.atHandle(thenearestVehicle); testob.explode(); Nope. Neither works. Use explode() in server-side 1 Link to comment Share on other sites More sharing options...
Flow Posted March 13, 2018 Author Share Posted March 13, 2018 vor 11 Stunden schrieb Captien: Just Explode it directly, No need to get its handle. mp.game.vehicle.getClosestVehicle gets the vehicle object direct, so just explode it directly It is for a quick example. I am aiming to repair, lock and refuel vehicle from the outside where i need it's handle. Its no problem while sitting IN the vehicle but i want to lock/unlock it from outside. That's the goal. Best regards Link to comment Share on other sites More sharing options...
Django93 Posted March 13, 2018 Share Posted March 13, 2018 (edited) This function is intended to simulate that the tires break when fired. getClosestVehicle() returns only one int, not the vehicle handle. Here is my working client-side script: mp.events.add('playerWeaponShot', (targetPosition) => { let nearestVehicle = mp.game.vehicle.getClosestVehicle(targetPosition.x, targetPosition.y, targetPosition.z, 2, 0, 70); if( nearestVehicle != 0 ) { // chance of damage if( Math.floor((Math.random() * 100) + 1) < 25 ) { for (var i = 0; i < 4; i++) { var DamagedWheel = Math.floor((Math.random() * 9)); mp.vehicles.atHandle(nearestVehicle).setTyreBurst(DamagedWheel, true, 1000); } } } }); But I think it's easier and safer to run these functions on the server side. Here is an example from my server: mp.events.add('vehicle_toggle_lock', (Player) => { var NearbyVehicles = []; mp.vehicles.forEachInRange(Player.position, 2.5, (NerbyVehicle) => { NearbyVehicles.push(NerbyVehicle); }); // sort the vehicles by range (0 is closest to the player) NearbyVehicles.sort(function(a, b){return b.dist(Player.position)-a.dist(Player.position)}); if( NearbyVehicles.length > 0 ) { if( NearbyVehicles[0].locked ) { NearbyVehicles[0].locked = false; Player.notify("You ~g~unlocked the vehicle."); // MySQL_Conn.query("UPDATE vehicles SET veh_locked='0' WHERE vehicle=?", [NearbyVehicles[0].data.id]); } else { NearbyVehicles[0].locked = true; Player.notify("You ~r~locked the vehicle."); // MySQL_Conn.query("UPDATE vehicles SET veh_locked='1' WHERE vehicle=?", [NearbyVehicles[0].data.id]); } } }); Edited March 13, 2018 by Django93 added serverside script solution 1 Link to comment Share on other sites More sharing options...
Bryan63 Posted March 13, 2018 Share Posted March 13, 2018 1 час назад, Django93 сказал: But I think it's easier and safer to run these functions on the server side. Here is an example from my server: mp.events.add('vehicle_toggle_lock', (Player) => { var NearbyVehicles = []; mp.vehicles.forEachInRange(Player.position, 2.5, (NerbyVehicle) => { NearbyVehicles.push(NerbyVehicle); }); // sort the vehicles by range (0 is closest to the player) NearbyVehicles.sort(function(a, b){return b.dist(Player.position)-a.dist(Player.position)}); if( NearbyVehicles.length > 0 ) { if( NearbyVehicles[0].locked ) { NearbyVehicles[0].locked = false; Player.notify("You ~g~unlocked the vehicle."); // MySQL_Conn.query("UPDATE vehicles SET veh_locked='0' WHERE vehicle=?", [NearbyVehicles[0].data.id]); } else { NearbyVehicles[0].locked = true; Player.notify("You ~r~locked the vehicle."); // MySQL_Conn.query("UPDATE vehicles SET veh_locked='1' WHERE vehicle=?", [NearbyVehicles[0].data.id]); } } }); What for? If there is a special tool for this? //client-side let localPlayer = mp.players.local let idVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 10, 0, 70) let vehicle = mp.vehicles.atHandle(idVehicle) mp.events.callRemote("lockVehicle", vehicle) //server-side mp.events.add("lockVehicle", (player, vehicle) => vehicle.locked = !vehicle.locked) 1 Link to comment Share on other sites More sharing options...
Django93 Posted March 13, 2018 Share Posted March 13, 2018 On my server all data is stored in Mysql. When a player wants to lock or unlock a vehicle, it is checked if he has a key for the vehicle. But I cut the part out. Everyone has to know that himself, how he wants to implement it. Both varieties work. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now