Jump to content

Destroy Objects from client to server


Recommended Posts

Hi, how to destroy objects created in client side, but i want to destroy it for all players (server side)

 

-- Client
triggerServerEvent("destroyBox", localPlayer, element)

-- Server
function destroyBox(element)
  destroyElement(element)
end
addEvent("destroyBox", true)
addEventHandler( "destroyBox", resourceRoot, destroyBox)

 

Link to comment
hi here i made an example
--client
local theObject = createObject(18450, 0, 0, 2) --test object

addEvent("destroyObject", true)
addEventHandler("destroyObject", root,
    function()
       destroyElement(theObject) --destroy theObject
    end
)

--server
triggerClientEvent(root, "destroyObject", root) --destroy theObject object for everyone

addCommandHandler("destroyobject", 
	function(player)
       triggerClientEvent(player, "destroyObject", player) -- destroy that object for the person who wrote this command
    end
)
Link to comment

Or if you have multiple objects in client-side, you can use this variant (not tested)
 

--// Client

local myObj1 = createObject(18450, 0, 0, 2)
local myObj2 = createObject(18450, 100, 200, 20)

function DestroyObject(theObject)
   local theObject = theObject or false
   if theObject ~= false then
      if getElementType(theObject) == "object" then
         destroyElement(theObject)
      end
   end
end
addEvent("DestroyObject", true)
addEventHandler("DestroyObject", root, DestroyObject)


--// Server


function DeleteClientObjects(thePlayer, cmd, objName)
   if objName then
      triggerClientEvent(root, "DestroyObject", root, objName)
   end
end
addCommandHandler("delobject", DeleteClientObjects)


--// Command Example

/delobject myObj1   (it will destroy the myObj1 from client-side)

 

Link to comment

the problem is that this is done by onClientClick in my script

-- Client
function takeBox(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, element) 
    if element and getElementType(element) == "object" and button == "left" and state == "down" then 
      local x, y, z = getElementPosition(localPlayer)
      local x1, y1, z1 = getElementPosition(element) 
      local distance = getDistanceBetweenPoints3D(x,y,z,x1,y1,z1)
      if distance < 3 then
        if getElementModel(element) == 2969 then
          setElementData(localPlayer, "boxes", boxes+1)
          destroyElement(element)
        end
      end  
    end
  end
end 

How to destroy this element for all players

Link to comment
12 minutes ago, Burak5312 said:

On which side are the objects created, server or client?

-- Client
local boxes = {
  {2969, 2484.1240234375, -1519.0988769531, 23.9921875, 0, 0, 55.477935791016},
}

-- //////////////////// LOAD BOXES //////////////////// --
for i,v in pairs(boxes) do
  local r = math.random(1,270)
  if isElement(obj) then
    destroyElement(obj)
  end
  obj = createObject(v[1], v[2], v[3], v[4]-0.9, 0, 0, r)
end

 

Link to comment
-- Client
local boxes = {
  {2969, 2484.1240234375, -1519.0988769531, 23.9921875, 0, 0, 55.477935791016},
}
-- //////////////////// LOAD BOXES //////////////////// --
for i,v in ipairs(boxes) do
   local r = math.random(1,270)
   if isElement(boxes[i][8]) then
      destroyElement(boxes[i][8])
   end
   boxes[i][8]= createObject(v[1], v[2], v[3], v[4]-0.9, 0, 0, r)
end

-- Client
function takeBox(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, element) 
    if element and getElementType(element) == "object" and button == "left" and state == "down" then 
      local x, y, z = getElementPosition(localPlayer)
      local x1, y1, z1 = getElementPosition(element) 
      local distance = getDistanceBetweenPoints3D(x,y,z,x1,y1,z1)
      if distance < 3 then
        if getElementModel(element) == 2969 then
           setElementData(localPlayer, "boxes", boxes+1)
           for index, v in ipairs(boxes) do 
              if v[8] and isElement(v[8]) then
                 if v[8] == element then 
                    triggerClientEvent("destroyBox", localP, index)
                    break 
                 end 
              end 
           end 
        end
      end     
    end
  end
end 

--- server -- 
addEvent("destroyBox", true)
addEventHandler("destroyBox", root, 
   function(index)
      triggerClientEvent(root, "destroyBox", root, index)
   end 
 )

 -- client --- 
 addEvent("destroyBox", true)
addEventHandler("destroyBox", root, 
   function(index)
      if boxes[index] and boxes[index][8] and isElement( boxes[index][8]) then
         destroyElement( boxes[index][8] )
      end 
   end 
 )


 

 

Edited by alex17"
Link to comment
39 minutes ago, Pickanothername said:

If i try pickup the box
image.png

-- Line #99
triggerClientEvent("destroyBox", localPlayer, index)

@alex17"

my bad 

-- Client
local boxes = {
  {2969, 2484.1240234375, -1519.0988769531, 23.9921875, 0, 0, 55.477935791016},
}
-- //////////////////// LOAD BOXES //////////////////// --
for i,v in ipairs(boxes) do
   local r = math.random(1,270)
   if isElement(boxes[i][8]) then
      destroyElement(boxes[i][8])
   end
   boxes[i][8]= createObject(v[1], v[2], v[3], v[4]-0.9, 0, 0, r)
end

-- Client
function takeBox(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, element) 
    if element and getElementType(element) == "object" and button == "left" and state == "down" then 
      local x, y, z = getElementPosition(localPlayer)
      local x1, y1, z1 = getElementPosition(element) 
      local distance = getDistanceBetweenPoints3D(x,y,z,x1,y1,z1)
      if distance < 3 then
        if getElementModel(element) == 2969 then
           setElementData(localPlayer, "boxes", boxes+1)
           for index, v in ipairs(boxes) do 
              if v[8] and isElement(v[8]) then
                 if v[8] == element then 
                    triggerServerEvent("destroyBox", localP, index)
                    break 
                 end 
              end 
           end 
        end
      end     
    end
  end
end 

--- server -- 
addEvent("destroyBox", true)
addEventHandler("destroyBox", root, 
   function(index)
      triggerClientEvent(root, "destroyBox", root, index)
   end 
 )

 -- client --- 
 addEvent("destroyBox", true)
addEventHandler("destroyBox", root, 
   function(index)
      if boxes[index] and boxes[index][8] and isElement( boxes[index][8]) then
         destroyElement( boxes[index][8] )
      end 
   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...