Jump to content

Vector 3 ( a user data value )


Zcraks

Recommended Posts

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

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)

 

  • Like 1
Link to comment
  • Moderators
-- 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 by IIYAMA
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...