Jump to content

Destroy objects


xyyy018

Recommended Posts

I have this code in client-side

  
addEvent("getGroundPosition", true) 
function getGround(x, y, z) 
    local z2 = getGroundPosition(x,y,z) 
    local o = createObject(356, x, y, z2) 
end 
addEventHandler("getGroundPosition", getRootElement(), getGround) 
  

And this code for server-side

  
addCommandHandler("bot", botTest) 
addEvent("getGroundPosition", true) 
function botDead(attacker, weapon, bodypart) 
    local x, y, z = getElementPosition(source) 
    triggerClientEvent("getGroundPosition", attacker, x, y, z) 
end 
  

I bind on key F remove all objects with ID 356 (in server-side)

  
function getItems(player) 
    local x, y, z = getElementPosition(player) 
    outputChatBox("try_one", player) 
    for _,p in ipairs(getElementsByType('object')) do 
        outputChatBox("try_two_one", player) 
        if isElementInRange(p, x, y, z, 50) then 
            outputChatBox("try_two", player) 
            if(getElementModel(p) == 356) then 
                outputChatBox("try_three", player) 
                destroyElement(p) 
            end 
        end 
    end 
end 
  

This only works if you create an object on the server side, but then I can not use getGroundPosition. However, if the object creation will be on the client side, this code does not work

Link to comment

Can you post your full code, I fail to understand what you're trying to do with mis-leading triggers/functions and events.

Edit: I wanted to help you out and I wasn't much aware of this function neither your code, so I edited a few things to try and get it to work, and here's what I concluded:

  
--Client Side: 
addEvent("getGroundPosition", true) 
function getGround(x, y, z) 
    local z2 = getGroundPosition(x,y,z) 
    triggerServerEvent("sendDataToServer", localPlayer, z2) 
    outputChatBox("triggered") 
end 
addEventHandler("getGroundPosition", getRootElement(), getGround) 
  

  
--Server Side: 
function botTest(plr) 
    x, y, z = getElementPosition(plr) 
    ped = createPed(217, x, y, z) 
    addEventHandler("onPedWasted", ped, botDead) 
end 
addCommandHandler("bot", botTest) 
  
function botDead(bleh, killer, weapon, bodypart) 
    local x, y, z = getElementPosition(source) 
    triggerClientEvent("getGroundPosition", killer, x, y, z) 
end 
  
function sendDataToServer(data) 
    o = createObject(356, x, y, data) 
end 
addEvent("sendDataToServer", true) 
addEventHandler("sendDataToServer", root, sendDataToServer) 
  
function getItems(player) 
    local x, y, z = getElementPosition(player) 
    outputChatBox("try_one", player) 
    for _, p in ipairs(getElementsByType("object")) do 
        outputChatBox("try_two_one", root) 
        if (isElement(p)) then 
            outputChatBox("hey") 
            if isElementInRange(p, x, y, z, 50) then 
                outputChatBox("try_two", player) 
                if(getElementModel(p) == 356) then 
                    outputChatBox("try_three", player) 
                    destroyElement(p) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("hey", getItems) 
  
function isElementInRange(ele, x, y, z, range) 
   if isElement(ele) and type(x) == "number" and type(y) == "number" and type(z) == "number" and type(range) == "number" then 
      return getDistanceBetweenPoints3D(x, y, z, getElementPosition(ele)) <= range -- returns true if it the range of the element to the main point is smaller than (or as big as) the maximum range. 
   end 
   return false 
end 
  

I added the debug notations, which you can remove, I added a few command handlers to call the codes, and that function "isElementWithinRange", wasn't working, so I added MTA's provided code for it and added it into the script itself. You must use /debugscript 3 to check for errors, and you must know triggering events can transfer data, numeric or string. What I did was triggered the client event to go and fetch the data, then trigger the server event with the data and triggered creating the object in the server side.

Your code can be optimised much more with tables too, and you could have made things easier. But here's the outcome to your issue. Hope I helped. Also change those variables to local variables, just made it like that cause I was trying to figure out the issue.

Link to comment

Note that the object must be near the player in order to use getGroundPosition otherwise it returns false.

Wiki:

It is required that the point is near enough to the local player so that it's within the area where collision data is loaded. If this is not the case, an incorrect position will be returned.
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...