local VEHICLE_COLLISIONS = {} VEHICLE_COLLISIONS.CACHE = {}; function VEHICLE_COLLISIONS:registerNewElement(Element) if not isElement(Element) then return end if not isElementStreamedIn(Element) then return end local ElementType = getElementType(Element); if ElementType ~= 'player' and ElementType ~= 'vehicle' then return end VEHICLE_COLLISIONS.CACHE[Element] = ElementType ~= 'player' and getElementData(Element, 'vehicle:lowCol') or false; -- Cache the lowCol state if VEHICLE_COLLISIONS.CACHE[Element] then if ElementType ~= 'player' then setElementAlpha(Element, 200) end for Element2, State in next, VEHICLE_COLLISIONS.CACHE, nil do if Element ~= Element2 and isElement(Element2) then if isElementCollidableWith(Element2, Element) then setElementCollidableWith(Element2, Element, false) end if isElementCollidableWith(Element, Element2) then setElementCollidableWith(Element, Element2, false) end end end else for Element2, State in next, VEHICLE_COLLISIONS.CACHE, nil do if Element ~= Element2 and isElement(Element2) then if State then if isElementCollidableWith(Element2, Element) then setElementCollidableWith(Element2, Element, false) end if isElementCollidableWith(Element2, Element) then setElementCollidableWith(Element2, Element, false) end end end end end end function VEHICLE_COLLISIONS:unregisterElement(Element) local ElementType = isElement(Element) and getElementType(Element); if ElementType and (ElementType ~= 'player' and ElementType ~= 'vehicle') then return end VEHICLE_COLLISIONS.CACHE[Element] = nil if isElement(Element) then if ElementType ~= 'player' then setElementAlpha(Element, 255) end for Element2, State in next, VEHICLE_COLLISIONS.CACHE, nil do if Element ~= Element2 and isElement(Element2) then if not State then if not isElementCollidableWith(Element2, Element) then setElementCollidableWith(Element2, Element, true) end if not isElementCollidableWith(Element, Element2) then setElementCollidableWith(Element, Element2, true) end end end end end end addEventHandler('onClientElementStreamIn', root, function() VEHICLE_COLLISIONS:registerNewElement(source) end) addEventHandler('onClientElementStreamOut', root, function() VEHICLE_COLLISIONS:unregisterElement(source) end) addEventHandler('onClientElementDestroy', root, function() VEHICLE_COLLISIONS:unregisterElement(source) end) addEventHandler('onClientVehicleEnter', root, function(player, seat) local source, player = source, player; if not isElement(source) then return end if not isElementStreamedIn(source) then return end if isElement(player) then VEHICLE_COLLISIONS:registerNewElement(player) end VEHICLE_COLLISIONS:registerNewElement(source) end, true, 'low-99') addEventHandler('onClientElementDataChange', root, function(dataName, oldValue, newValue) if not isElement(source) then return end if getElementType(source) ~= 'vehicle' then return end if dataName ~= 'vehicle:lowCol' then return end if newValue then if VEHICLE_COLLISIONS.CACHE[source] then return end if not isElementStreamedIn(source) then return end VEHICLE_COLLISIONS:registerNewElement(source) else if not VEHICLE_COLLISIONS.CACHE[source] then return end VEHICLE_COLLISIONS:unregisterElement(source) end end) addEventHandler('onClientResourceStart', resourceRoot, function() local Elements = {} local vehicles = getElementsByType('vehicle', root, true) local players = getElementsByType('player', root, true) for i, v in next, vehicles, nil do table.insert(Elements, v) end for i, v in next, players, nil do table.insert(Elements, v) end table.sort(Elements, function(a, b) return getElementData(a, 'vehicle:lowCol') and not getElementData(b, 'vehicle:lowCol') end) for _, Element in next, Elements, nil do VEHICLE_COLLISIONS:registerNewElement(Element) end end) addEventHandler('onClientResourceStop', resourceRoot, function() for Element in next, VEHICLE_COLLISIONS.CACHE, nil do VEHICLE_COLLISIONS:unregisterElement(Element) end end) setTimer(function() for elementA, state in next, VEHICLE_COLLISIONS.CACHE, nil do if isElement(elementA) and state then for elementB, stateB in next, VEHICLE_COLLISIONS.CACHE, nil do if isElement(elementB) and elementA ~= elementB then if isElementCollidableWith(elementA, elementB) then setElementCollidableWith(elementA, elementB, false) end if isElementCollidableWith(elementB, elementA) then setElementCollidableWith(elementB, elementA, false) end end end end end end, 1500, 0)