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
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.