Jump to content

detachElement help


jkub

Recommended Posts

Posted

I am making a small attach script And I need help with detaching all the objects that has been attached. This only works with one object and it is the last object attached wats the prob?

function detachObject ( player ) 
local playerVehicle = getPlayerOccupiedVehicle ( player ) 
    detachElementFromElement ( object[player], playerVehicle ) 
    destroyElement ( object[player] ) 
    outputChatBox ( "objects detached!", player, 0, 255, 0 ) 
end 
addCommandHandler ( "detach", detachObject ) 

Ive tried out getAttachedElements and tried that out didnt seem to help

Posted

I guess that when you attach, you write object[player] = the attached object.. So every time you attach, you overwrite object[player].. You can use object[player] = {} and then put table.insert(object[player], theElement) or something like that, so each of the attached elements would have it's own identifier.. Currently you are just overwriting the variable every time, since the player is constantly the same for the same client.

Posted

Here is the current script i got now

local playerobj = {} 
addCommandHandler ( "attach", 
    function ( player, cmd, objid, offsetx, offsety, offsetz, offsetrx, offsetry, offsetrz ) 
    local playerVehicle = getPlayerOccupiedVehicle ( player ) 
        playerobj[player] = createObject (tonumber ( objid ), 0, 0, 0 ) 
        attachElementToElement ( playerobj[player], playerVehicle, offsetx, offsety, offsetz, offsetrx, offsetry, offsetrz ) 
    end 
) 
  
function detachAll ( player ) 
local playerVehicle = getPlayerOccupiedVehicle ( player ) 
    for _, playerobj in pairs ( getElementsByType ( "object" ) ) do 
        destroyElement ( playerobj ) 
    end 
end 
  
addCommandHandler ( "detach", detachAll ) 
     

Now the 2 major problems with this is

1. The detach will only work a couple times and then It will stop working

2. The detach > detaches objects from other players vehicles that they have attached

Wat Now... I put it in a table I think

Posted

after some more testing i figured out that it was not just other attached objects it was deleting... it is deleting all objects... It removed A large ramp I had loaded on my map

when I did the detach command it removed my ramp I had made for a map from another script

Posted

Well you could use getAttachedElements ( element theElement ) (link)

And then check to see if the elements attached are objects then use detachElements on the objects.

This will only work if you want to detach all objects from the vehicle...

Posted

Ok this is what I got right now. It seems to work so far but I havent got chance to test it real far?>

Does it look right?

function detachAll ( player ) 
local playerVehicle = getPlayerOccupiedVehicle ( player ) 
local attachedObjects = getAttachedElements ( playerVehicle ) 
    for k, v in ipairs ( attachedObjects ) do 
        destroyElement ( v ) 
    end 
end 
  
addCommandHandler ( "detach", detachAll ) 
     

Posted

nope... after a couple times of detaching it still stops working and U cant remove the objects. Ive even tried stopping and restarting the script... anything else I can do and/or is there anything else I am not doing right? I though destroying "v" as the attachedElements would do it. idk

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