Stijger Posted March 7, 2012 Share Posted March 7, 2012 Hello again, Apparently I really suck at scripting, so i need your help again guys. I got this code, and what I want is if the trailer and truck get to far from eachother on trailerdetach (so on like big high speed crash) they blow up. function trailerGone ( theTruck ) theTruckVehicle = theTruck theTruckTrailer = source trailerx, trailery, trailerz = getElementPosition(source) vehiclex, vehicley, vehiclez = getElementPosition (theTruckVehicle) outputChatBox ("X coordinate of trailer is " ..trailerx) outputChatBox ("X coordinate of vehicle is " ..vehiclex) dist = getDistanceBetweenPoints2D (vehiclex, vehicley, trialerx, trailery) if(getDistanceBetweenPoints2D (vehiclex, vehicley, trialerx, trailery) <= 20) then blowVehicle ( theTruckVehicle ) blowVehicle ( theTruckTrailer ) end end addEventHandler( "onTrailerDetach", getRootElement(), trailerGone ) Gives these errors: WARNING: race-convoytrucking\trailerattach.lua:8: Bad argument @ 'getDistanceBetweenPoints2D' WARNING: race-convoytrucking\trailerattach.lua:9: Bad argument @ 'getDistanceBetweenPoints2D' ERROR: race-convoytrucking\trailerattach.lua:9: attempt to compare boolean with number Now the problem is, I get a bad argument on the trailerx when it's compared to the vehiclex. (same with the y coordinates i guess) the outputbox trailerx and vehiclex is working fine thou, so why doesn't it work in the getDistanceBetweenPoints2D? edit: the source and theTruckTrailer part I switched already, don't make any difference. Link to comment
Kenix Posted March 7, 2012 Share Posted March 7, 2012 function trailerGone ( uTruck ) local trailerx, trailery, _ = getElementPosition( source ) local vehiclex, vehicley, _ = getElementPosition ( uTruck ) outputChatBox ( "X coordinate of trailer is " .. tostring( trailerx ) ) outputChatBox ( "X coordinate of vehicle is " .. tostring( vehiclex ) ) local dist = getDistanceBetweenPoints2D ( vehiclex, vehicley, trailerx, trailery ) if dist <= 20 then blowVehicle ( uTruck ) blowVehicle ( source ) end end addEventHandler( "onTrailerDetach", root, trailerGone ) Because you use not defined variable trialerx in 9 line. Link to comment
Stijger Posted March 7, 2012 Author Share Posted March 7, 2012 Thank you so much, it works. so that tostring turns my variable into a string?? Cause i read something about it, but didn't quite understand it. Link to comment
Kenix Posted March 7, 2012 Share Posted March 7, 2012 No problem. In old version mta server/client outputChatBox/outputDebugString not convert all variables to string. You need use tostring in function outputChatBox/outputDebugString for convert variables to string. If you not convert, it return false and bad argument and not output. Link to comment
Stijger Posted April 13, 2012 Author Share Posted April 13, 2012 Hey, I need some help again. Iam still working on the trailer distance thing, but now i thought well wouldn't it be more awesome if you can dettach the trailer, but when you get too far from it, you blow up. So something like that checks the distance between the two vehicles a lot and then when you get too far, it goes BOOM!! Tried this but it don't remember the trailer after the dettachment. root = getRootElement () function attachedTrailer () local theTruckVehicle = getPedOccupiedVehicle ( getLocalPlayer () ) local theTruckTrailer = getVehicleTowedByVehicle ( theTruckVehicle ) local vehiclex, vehicley, vehiclez = getElementPosition (theTruckVehicle) local trailerx, trailery, trailerz = getElementPosition(theTruckTrailer) local dist = getDistanceBetweenPoints2D (trailerx, trailery, vehiclex, vehicley) if dist >= 20 then blowVehicle ( theTruckVehicle ) blowVehicle ( theTruckTrailer ) end end addEventHandler ( "onClientRender", root, attachedTrailer ) Link to comment
Castillo Posted April 13, 2012 Share Posted April 13, 2012 That must be because you're using getVehicleTowedByVehicle, after you detach it, the function will return nothing, because you no longer have a trailer attached. Link to comment
Stijger Posted April 13, 2012 Author Share Posted April 13, 2012 Any idea how i should get the towed vehicle then? Coz I have no idea. The player vehicle is no problem ofcourse. Link to comment
Castillo Posted April 13, 2012 Share Posted April 13, 2012 What you're trying to do is to check if the vehicle is far from the trailer, that means the trailer is no longer attached, so you'll have to check the trailer, you can't get the "attached" trailer because there isn't. Link to comment
Stijger Posted April 13, 2012 Author Share Posted April 13, 2012 yeah I get that, but how should i get it then. Any suggestions? Link to comment
Castillo Posted April 13, 2012 Share Posted April 13, 2012 You could set a ID to the trailer, e.g the player serial then get the element by the ID using: getElementByID. Link to comment
Stijger Posted April 13, 2012 Author Share Posted April 13, 2012 hmm okay good idea, thank you again Castillo. I will try that and maybe get back on it 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