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)