gerlachmax98 Posted January 24, 2013 Share Posted January 24, 2013 How i get the nearest vehicle. For example is stand in front of a vehicle, and with the command /open you can open the vehicle bonnet from the nearest vehicle. Please help... Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 This client-sided function should get you the nearest vehicle to the local player. function getNearestVeicle() local x, y, z = getElementPosition( localPlayer ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v end end return nearestVehicle or false end Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 [2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:85: Bad argument @ 'getElementPosition' [Expected element at argument 1, got nil] [2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:89: Bad argument @ 'getDistanceBetweenPoints3D' [2013-01-24 20:51:05] ERROR: vio_lite\reparieren\server.lua:90: attempt to perform arithmetic on local 'distance' (a boolean value) I have it now so: function getNearestVeicle() local x, y, z = getElementPosition( localPlayer ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v setElementHealth ( v, 800 ) end end return nearestVehicle or false end addCommandHandler("repaircar", getNearestVeicle) Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 As I said, this function is only for client-side, to use it on server-side, i have to modify it a bit, i will do it later ( cuz im from my phone now ). And it won't repair your vehicle, it just gets the nearest vehicle to the local player. An example how to use it: fixVehicle( getNearestVeicle() ) I've just noticed that a word is missing from the name of the function, of course it will work with this name too, just doesn't look good Link to comment
Cadu12 Posted January 24, 2013 Share Posted January 24, 2013 Try this: function getNearestVeicle(thePlayer) local x, y, z = getElementPosition( thePlayer ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v setElementHealth ( v, 800 ) end end return nearestVehicle or false end addCommandHandler("repaircar", getNearestVeicle) csiguusz: You're wrong, this function isnt only for client-side, thats for both. Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 (edited) Because i have a lot of time now, a made it! This should work on server-side. But i think it would be better to get the nearest veh on client side, because so many calculations can have an impact on the server's performance. function getNearestVehicle( player ) local x, y, z = getElementPosition( player ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v end end return nearestVehicle or false end function doSomethingWithTheNearestVeh( player ) local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command if vehicle then fixVehicle( vehicle ) -- fix the nearest vehicle. this is just an example, you can do here anything else end end addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) Edited January 24, 2013 by Guest Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 @Cadu12: the first function that i wrote here can only be used on client-side. With a little modification (what i did now) it's working on server-side too. Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 Thank you so much!!!! How i can set a timer, when the vehicle is repairing. For a half minute. Can be there a animation when the vehicle is repairing ? (Half minute) Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 Hm? What kind on animation do you mean? Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 "wash_up" maybe? Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 Oh, you want to set an animation to the player who repairs the car! You can use setPedAnimation for this. Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 Yes, but i need a animation on a timer... I do not know how to do it... Link to comment
Lloyd Logan Posted January 24, 2013 Share Posted January 24, 2013 Yes, but i need a animation on a timer...I do not know how to do it... setTimer(setPedAnimation, 30000, 1, pedyouwanttosettheanimationtoo e.g thePlayer, "GroupName", "AnimationName") Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 setPedAnimation( player, "some", "anim") -- set your animation to a player setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds Link to comment
Lloyd Logan Posted January 24, 2013 Share Posted January 24, 2013 setPedAnimation( player, "some", "anim") -- set your animation to a player setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds Oh, wrong thing sorry, misread that Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 so? function getNearestVehicle( player ) local x, y, z = getElementPosition( player ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v end end return nearestVehicle or false end function doSomethingWithTheNearestVeh( player ) local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command if vehicle then setPedAnimation( player, "wash_up", "anim") -- set your animation to a player setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else end end addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) Link to comment
Lloyd Logan Posted January 24, 2013 Share Posted January 24, 2013 so? function getNearestVehicle( player ) local x, y, z = getElementPosition( player ) local prevDistance local nearestVehicle for i, v in ipairs( getElementsByType( "vehicle" ) ) do local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) if distance <= ( prevDistance or distance + 1 ) then prevDistance = distance nearestVehicle = v end end return nearestVehicle or false end function doSomethingWithTheNearestVeh( player ) local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command if vehicle then setPedAnimation( player, "wash_up", "anim") -- set your animation to a player setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else end end addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) Does it work? Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 I think, the animation what you want to set doesn't exist. And you can delete my comments from the script if you don't need them. I just wrote them for you, to help to understand what those lines are for. Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 https://wiki.multitheftauto.com/wiki/Animations this exist, but there is no Animation... Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 https://wiki.multitheftauto.com/wiki/Animationsthis exist, but there is no Animation... The "wash_up" anim is in the "BD_FIRE" block, so use setPedAnimation( player, "BD_FIRE", "wash_up" ) Link to comment
gerlachmax98 Posted January 24, 2013 Author Share Posted January 24, 2013 okay ty can you make it so that you may standing in front of the car and not as driver? Link to comment
csiguusz Posted January 24, 2013 Share Posted January 24, 2013 You can check if the player is in any vehicle so: if not getPedOccupiedVehicle( player ) then -- the player is on foot end You can get the driver of a vehicle with the getVehicleController function. It's possible to check if you are standing in front of a vehicle or not somehow ( i think with getElementMatrix ) but i cant' help u with this, i don't understand matrixes very well. Link to comment
gerlachmax98 Posted January 25, 2013 Author Share Posted January 25, 2013 Thank-you ) In front of the vehicle will be good, but it will Not work to all vehicle, or? Link to comment
csiguusz Posted January 25, 2013 Share Posted January 25, 2013 This function, what i gave you checks the distance between the given player and every vehicles on the map. So it will work with every vehicles. It can even return a vehicle that is in an another city because it can also be the nearest. Link to comment
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