Jump to content

destroyelement


Recommended Posts

hi now i have script when player press 1

will create this

-- server
local myobject = createObject (8873,  myx, myy, myz)
local mycol = createColSphere ( myx, myy, myz, 320 )
setElementData(mycol,'col',myobject)
setElementData(mycol,'colplayer',plr)

i want when player quit destroy all elements ( not last elements only ) created by him

Link to comment
  • Other Languages Moderators

Hello.

First of all, you need a table to store all the player elements. It's a way easier to destroy futher elements. The player element would be the table index and its value would be your object and your colshape.

I make an example:

local playerData = {} -- We define a table to store all player elements as a key.

function createObjectAndColShape(player)
    -- I don't now what these "myx", "myy" and "myz" are, I presume that's the player's position or matrix. I will use player's position
    local myx, myy, myz = getElementPosition(player)
    local object = createObject(8873, myx, myy, myz)
    local colshape = createColSphere(myx, myy, myz, 320)

    setElementData(colshape, "col", object)
    setElementData(colshape, "colplayer", player)

    playerData[player] = {object, colshape} -- Here we set the index as being the player, and it stores the object and the colshape
end

function destroyObjectAndColShape(player)
    if type(playerData[player]) ~= "table" then
        return false
    end

    for player, element in pairs(playerData[player]) do
        if isElement(element) then
            destroyElement(element)
        end
    end

    playerData[player] = nil
end

addEventHandler("onPlayerQuit", root, function() -- When the player quits the server, so we call that function to destroy its elements and free memory
    destroyObjectAndColShape(source)
end)

addCommandHandler("cp", function(player) -- Just a command /cp to create the objects, you can use this function on your bindKey function
    createObjectAndColShape(player)
end)

 

Link to comment
29 minutes ago, andr0xy said:

Hello.

First of all, you need a table to store all the player elements. It's a way easier to destroy futher elements. The player element would be the table index and its value would be your object and your colshape.

I make an example:


local playerData = {} -- We define a table to store all player elements as a key.

function createObjectAndColShape(player)
    -- I don't now what these "myx", "myy" and "myz" are, I presume that's the player's position or matrix. I will use player's position
    local myx, myy, myz = getElementPosition(player)
    local object = createObject(8873, myx, myy, myz)
    local colshape = createColSphere(myx, myy, myz, 320)

    setElementData(colshape, "col", object)
    setElementData(colshape, "colplayer", player)

    playerData[player] = {object, colshape} -- Here we set the index as being the player, and it stores the object and the colshape
end

function destroyObjectAndColShape(player)
    if type(playerData[player]) ~= "table" then
        return false
    end

    for player, element in pairs(playerData[player]) do
        if isElement(element) then
            destroyElement(element)
        end
    end

    playerData[player] = nil
end

addEventHandler("onPlayerQuit", root, function() -- When the player quits the server, so we call that function to destroy its elements and free memory
    destroyObjectAndColShape(source)
end)

addCommandHandler("cp", function(player) -- Just a command /cp to create the objects, you can use this function on your bindKey function
    createObjectAndColShape(player)
end)

 

work but destroy last element only not all object

Link to comment
  • Other Languages Moderators

Ah, got it. My bad. I've edited these functions:

function createObjectAndColShape(player)
    if not playerData[player] then -- If there is no player in the table index, then
        playerData[player] = {} -- we create a table to store all objects, it will accumulate the objects whenever this function is called
    end

    -- I don't now what these "myx", "myy" and "myz" are, I presume that's the player's position or matrix. I will use player's position
    local myx, myy, myz = getElementPosition(player)
    local object = createObject(8873, myx, myy, myz)
    local colshape = createColSphere(myx, myy, myz, 320)

    setElementData(colshape, "col", object)
    setElementData(colshape, "colplayer", player)

    table.insert(playerData[player], {object, colshape})
end

function destroyObjectAndColShape(player)
    if type(playerData[player]) ~= "table" then
        return false
    end

    for player, assets in pairs(playerData[player]) do
        for index, element in pairs(assets) do
            if isElement(element) then
                destroyElement(element)
            end
        end
    end

    playerData[player] = nil
end

 

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