Xwad Posted January 27, 2016 Share Posted January 27, 2016 (edited) Hi. I made a script that makes possible if i rotate the players camera to the right then the object will also rotate right. I mean that the object is rotating to that direction , where the players camera is looking. The only problem is that its not working.. Debugscript: lua:7: Bad Argumentum @ 'getElementRotation' [Expected element at argumentum 1,got nil] lua:8: Bad Argumentum @ 'getElementPosition' [Expected element at argumentum 1,got nil] lua:10: attempt to call global 'findRotation' (a nil value) Client.lua local test = createWeapon("m4", -2388.41, -581.15, 132.3) setWeaponState(test, "ready") function rotate () local rotX, rotY, rotZ = getElementRotation(source) local x, y, z = getElementPosition(source) local rotX, rotY, rotZ = getElementRotation(test) setElementRotation(test, findRotation(x,y,rotX,rotY) ) end function bindTheKeys () bindKey ( "h", "down", rotate ) end addEventHandler("onClientResourceStart", getRootElement(), bindTheKeys) Edited January 27, 2016 by Guest Link to comment
Mann56 Posted January 27, 2016 Share Posted January 27, 2016 First of all the variable test should not be local as you've called it in the function. Secondly the source you've mentioned might not be the player maybe use thePlayer. Sorry am not a computer can't check the code.... Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 I forgot to say that its client sided!!! I tryed but its the same error:/ Link to comment
Mr_Moose Posted January 27, 2016 Share Posted January 27, 2016 Variable test can be local, it's in the same file so that's not the problem. The function however is called by a keybind you set during resource start (noticed this is a client side resource) so you should use localPlayer instead of source. Your first two errors are caused by the fact that there is no source defined in that function. Moving on to the third error which is caused by an undefined function called "findRotation", you may have spelled wrong or forgot to define it, in any case there is no such function built in. Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 I changed the source to localPlayer. Its good now , but yeah theres still the error with the findRotation.. So its not defined? thats why its not working? So what should i do to fix that problem? Thanks. Link to comment
Army@1 Posted January 27, 2016 Share Posted January 27, 2016 Add this to your script. function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end And findRotation takes positions of elements as arguments. Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 Thanks Army@1!! Its still not working but theres no more error with setElementRotation, only a bad argumentum: lua:10: Bad Argumentum @ 'setElementRotation' [Expected number at argumentum 3, got none] Whats could be the problem now? Link to comment
Mr_Moose Posted January 27, 2016 Share Posted January 27, 2016 The problem here is that the function "findRotation" only returns the rotation in z axis while setElementRotation needs a vector with x,y and z, the solution is simple however. Change line 11 from this: setElementRotation(test, findRotation(x,y,rotX,rotY)) to this: setElementRotation(test, 0,0, findRotation(x,y,rotX,rotY)) Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 Now theres no error but its not working correctly:// The weapon is not rotating to the direction where the camera is looking Link to comment
Army@1 Posted January 27, 2016 Share Posted January 27, 2016 Try replacing this line setElementRotation(test, 0,0, findRotation(x,y,rotX,rotY)) with this setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) --or if the above doesn't work setElementRotation(test, 0, 0, getPedCameraRotation(localPlayer)) Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 setElementRotation(test, 0, 0, getPedCameraRotation(localPlayer)) Actualy its working but not correctly again:/ When i use this one then the weapon is always rotating to the opposite side.. I mean if i look with the camera to the right side then the weapon will rotate to the left side.. :setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) This one is rotating correctly but this has also a problem.. when i put this line to a function and start the function the then weapon is looking to the right automaticly and i cant rotate it..Please help Link to comment
Army@1 Posted January 27, 2016 Share Posted January 27, 2016 Try Using onClientRender for the latter. addEventHandler ( "onClientRender", root, function() z = getPedCameraRotation(localPlayer) ) setElementRotation(test, 0, 0, 360 - z) Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 sry sry sry its working i just changed the values now!! now its not moving to the left!! i changed this from this : setElementRotation(test, 0, 0, 360 - getPedCameraRotation(localPlayer)) to this: setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) Thanks man that you are helping me im really grateful!! But i have one more problem!! When i look up and down with the camera then the weapon is not rotating up and down!! Link to comment
Army@1 Posted January 27, 2016 Share Posted January 27, 2016 Where did you get this from? setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) That looks weird to me. Anyway, try this setElementRotation(test, 180 - getPedCameraRotation(localPlayer), 180 - getPedCameraRotation(localPlayer), 180 - getPedCameraRotation(localPlayer)) Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 its not working correctly. the weapon is fully rotated now and the rotating is always the opposet but wait i test it again Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 no its not working correctly:( the weapon is rotating pell-mell Link to comment
Xwad Posted January 27, 2016 Author Share Posted January 27, 2016 Why is it not rotating up and down? pls help setElementRotation(test, 0, 3.2, 96.7 - getPedCameraRotation(localPlayer)) 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