Zcraks Posted January 20, 2021 Share Posted January 20, 2021 addEventHandler ("onPlayerVehicleEnter", getRootElement(), function (theVehicle, seat, jacked) if theVehicle and seat == 0 then theVehicle:setData("lastPos", theVehicle:getPosition()) end end) setTimer(function() for k, veh in ipairs(getElementsByType("vehicle")) do if veh then local pos = veh:getPosition() local vec = veh:getData("lastPos") if pos and vec and distance then --[[ Error => ]] local value = getDistanceBetweenPoints3D(vec:getX(), vec:getY(), vec:getZ(), pos:getX(), pos:getY(), pos:getZ()) if value then ------------- end end end end end, 2000, 0) attempt to index local "vec" ( a user data value ) Link to comment
Tekken Posted January 20, 2021 Share Posted January 20, 2021 I am not familiarized to OOP but getElementPosition returns 3 floats and not a table that might be the problem. Link to comment
NeXuS™ Posted January 20, 2021 Share Posted January 20, 2021 Seems like restarting the resource causes the element datas to break. Adding a check on the side fixes the issue, but creates another issue, you gotta reenter your car after each resource restart. Also, I'm not sure how your code ran, as there is no "distance" value defined in your snippet. If it's needed, readd it in the line please. addEventHandler ("onPlayerVehicleEnter", getRootElement(), function (theVehicle, seat, jacked) if theVehicle and seat == 0 then theVehicle:setData("lastPos", theVehicle:getPosition()) end end) setTimer(function() for k, veh in ipairs(getElementsByType("vehicle")) do if veh then local pos = veh:getPosition() local vec = veh:getData("lastPos") if pos and vec and getUserdataType(vec) == "vector3" then local value = getDistanceBetweenPoints3D(vec:getX(), vec:getY(), vec:getZ(), pos:getX(), pos:getY(), pos:getZ()) if value then end end end end end, 2000, 0) 1 Link to comment
Moderators IIYAMA Posted January 20, 2021 Moderators Share Posted January 20, 2021 (edited) -- Vector3 only: function tableToVector (theTable) return Vector3(theTable.x, theTable.y, theTable.z) end function vectorToTable(theVector) return {x = theVector.x, y = theVector.y, z = theVector.z} end theVehicle:setData("lastPos", vectorToTable(theVehicle:getPosition())) ------ local vec = veh:getData("lastPos") if vec then vec = tableToVector(vec) end Elementdata will always make a (deep)copy of the data, but vectors can't be (deep)copied. You also can't send vectors with triggerServer/ClientEvent, because there is (deep)copy also applied. Turning vectors in to tables, will solve the issue. But it might be even better to remove the elementData when the resource stops. Edited January 20, 2021 by IIYAMA 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