Jump to content

[HELP!] Table Delete Element


Lalalu

Recommended Posts

Hello everyone,  I need your help with something please. I'm trying to create something like pick items and stuff like that, plants, mushrooms, etc etc.

This creates a table with Objects and Collisions. I want to know how can I delete just the collision that I hit and the object in that position as well, not the other elements in the table just the elements that I hit.

local sx,sy = guiGetScreenSize()
local px,py = 1280,720
local x,y = (sx/px), (sy/py)

playerSeta = getLocalPlayer()

local Pos_Seta = {
  {323.9013671875, 2490.8134765625, 15.484375},
  {335.341796875, 2486.669921875, 15.484375},
  {330.0751953125, 2496.310546875, 15.484375},
  {325, 2480.7265625, 15.484375}
}

for i,v in ipairs (Pos_Seta) do
  Obj_Seta = createObject(2427, v[1],v[2],v[3]) setObjectScale(Obj_Seta, 1.5)
  Col_Seta = createColSphere (v[1],v[2],v[3], 1.5)
  Blip_Seta = createBlip(v[1],v[2],v[3], 33, 2.5, 255, 0, 0, 255, 0, 100)	

addEventHandler( "onClientColShapeHit", Col_Seta,
    function(pSeta)
      if pSeta == playerSeta then
        addEventHandler('onClientRender', getRootElement(), Text_Seta)
        bindKey('F','down',StartSeta, playerSeta)
      end 
    end)

addEventHandler( "onClientColShapeLeave", Col_Seta, 
    function(pSeta) 
      if pSeta == playerSeta then
        removeEventHandler('onClientRender', getRootElement(), Text_Seta)
        unbindKey('F','down',StartSeta, playerSeta)
      end
    end)
  end

function StartSeta ()	
  triggerServerEvent ( "StartSeta", localPlayer)
  unbindKey('F','down',StartSeta, playerSeta)
end		

function Text_Seta ()
  dxDrawText("Use (F) to pick this item", x*481, y*483, x*800, y*501, tocolor(255, 255, 255, 255), 1.35, "default-bold", "center", "top", false, false, false, false, false)
end

 

Edited by Lalalu
Link to comment
  • Lalalu changed the title to [HELP!] Table Delete Element
  • Scripting Moderators
3 hours ago, Lalalu said:

This creates a table with Objects and Collisions. I want to know how can I delete just the collision that I hit and the object in that position as well, not the other elements in the table just the elements that I hit.

You're not storing them in any table?

Anyway, you don't need to use tables here. Instead, you can set the other elements (object and blip) as children of the collision shape using setElementParent. This will cause them to be automatically destroyed when the parent (colshape) is destroyed. You can use getElementChildren to retrieve the children of an element.
Here's an example:

addEventHandler("onClientResourceStart",resourceRoot,
function()
    for index, position in ipairs(table_pos) do
        local x, y, z = unpack(position)

        -- Create a collision shape
        local colshape = createColSqaure(x, y, z, 1.5)

        -- Create an object and set its parent to the collision shape
        local object = createObject(2424, x, y, z)
        setElementParent(object, colshape)

        addEventHandler("onClientColShapeHit", colshape, onHit)
    end
end)

function onHit(hitElement)
    if hitElement == localPlayer then
        -- The source of this event is the colshape that was hit

        -- Retrieve the object that is a child of the collision shape
        local object = getElementChildren(source, "object")[1] -- The [1] here is used to get the first element of the table.

        if isElement(object) then -- Check if the object still exists
            ...
        end

        destroyElement(source) -- Destroy the collision shape
        -- The children of the collision shape will be destroyed automatically
    end
end

 

3 hours ago, Lalalu said:
playerSeta = getLocalPlayer()

There is no need to use getLocalPlayer here, localPlayer is a predefined variable that refers to the local player. You should simply use the variable localPlayer directly.

Edited by xLive
  • Thanks 1
Link to comment
local sx,sy     = guiGetScreenSize()
local px,py     = 1280,720
local x, y      = (sx/px), (sy/py)
local localP    = getLocalPlayer() 
local data      = {}
local positions = {
  {323.9013671875, 2490.8134765625, 15.484375},
  {335.341796875, 2486.669921875, 15.484375},
  {330.0751953125, 2496.310546875, 15.484375},
  {325, 2480.7265625, 15.484375}
}

addEventHandler("onClientResourceStart", resourceRoot,
   function()
      for _, value in ipairs(positions) do 
         local object    = createObject(2427, value[1], value[2], value[3]) 
         local colsphere = createColSphere (value[1], value[2], value[3], 1.5)
         local blip      = createBlip(value[1], value[2], value[3], 33, 2.5, 255, 0, 0, 255, 0, 100)	
         data[colsphere] = {object = object, blip = blip, colsphere = colsphere}
         setObjectScale(value.object, 1.5)
         addEventHandler("onClientColShapeHit", colsphere, onClientColShapeHit)
         addEventHandler("onClientColShapeLeave", colsphere, onClientColShapeLeave)

      end 
   end 
)

function onClientColShapeHit(element, dimension)
   if not data[source] then return end 
   if element == localP and dimension then 
      bindKey('F','down', StartSeta, source)
      addEventHandler('onClientRender', getRootElement(), Text_Seta)
   end 
end 

function onClientColShapeLeave()
   unbindKey('F','down', StartSeta)
   removeEventHandler('onClientRender', getRootElement(), Text_Seta)
end 

function StartSeta(_, _, colsphere)
   if data[colsphere] then 
      unbindKey('F','down', StartSeta)
      removeEventHandler('onClientRender', getRootElement(), Text_Seta)
      removeEventHandler("onClientColShapeHit", colsphere, onClientColShapeHit)
      removeEventHandler("onClientColShapeLeave", colsphere, onClientColShapeLeave)
      destroyElement(data[colsphere].object)
      destroyElement(data[colsphere].blip)
      destroyElement(data[colsphere].colsphere)
      data[colsphere] = nil 
   end 
end 


function Text_Seta ()
  dxDrawText("Use (F) to pick this item", x*481, y*483, x*800, y*501, tocolor(255, 255, 255, 255), 1.35, "default-bold", "center", "top", false, false, false, false, false)
end

try this

  • Thanks 1
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...