Jump to content

[HELP] Dropweapon Error


Recommended Posts

Hello, thanks for entering my publication.
 I'll give you some context, I'm creating an inventory system for weapons, then you have the option to drop the weapon you have and another player can pick it up.

Now the problem I have is that when two players decide to throw a weapon, then you go and try to grab the other player's weapon, the weapon that I threw on the ground is eliminated but not the one that the second player threw.

 

here I leave my code...

 

local colShapes = {}

function DropWeapon()
local , , r = getElementRotation(source)
local x,y,z = getElementPosition(source) 
setPedAnimation(source, "BOMBER", "BOM_plant_loop",-1,true,false,false,false) 
setTimer(setPedAnimation, 1500, 1, source, false)
takeWeapon(source, 2)

    obj = createObject(333, x+math.sin(math.rad(-r)) * 1,y+math.cos(math.rad(-r)) * 1, z-1, 90)
    col = createColSphere(x+math.sin(math.rad(-r)) * 1,y+math.cos(math.rad(-r)) * 1,z, 0.5) 
    colShapes[col] = obj

    addEventHandler("onColShapeHit", col, function(hitter)
        if getElementType(hitter) == "player" then
            if colShapes[source] then
            outputChatBox("ENTER",hitter,255,255,0,true)

                bindKey(hitter,"F","down",TakeWeapon)
                --destroyElement(colShapes[source])
               --destroyElement(source)
            end
        end 
    end) 
    addEventHandler("onColShapeLeave", col, function(hitter)
        if getElementType(hitter) == "player" then
            if colShapes[source] then
            outputChatBox("EXIT",hitter,255,255,0,true)

                unbindKey(hitter,"F","down",TakeWeapon)
               -- colShapes[source] = nil
            end
        end 
    end) 
end 
addEvent ("dropKatana", true) addEventHandler ("dropKatana", root, DropWeapon)


function TakeWeapon(source) 
 if isElement(colShapes[col]) then
    colShapes[col] = obj
    colShapes[source] = nil

    giveWeapon(source, 2, 1, true)
    setPedAnimation(source, "BOMBER", "BOM_plant_loop",-1,true,false,false,false)     setTimer(setPedAnimation, 1500, 1, source, false)

    unbindKey(source,"F","down",TakeWeapon)
    destroyElement(colShapes[col])  destroyElement(source)
  end
end

 

thank you!!

Link to comment

Hi, try this

 

local colShapes = {}

function DropWeapon()
    local _, _, r = getElementRotation(source)
    local x, y, z = getElementPosition(source) 
    setPedAnimation(source, "BOMBER", "BOM_plant_loop", -1, true, false, false, false) 
    setTimer(setPedAnimation, 1500, 1, source, false)
    takeWeapon(source, 2)

    local obj = createObject(333, x + math.sin(math.rad(-r)) * 1, y + math.cos(math.rad(-r)) * 1, z - 1, 90)
    local col = createColSphere(x + math.sin(math.rad(-r)) * 1, y + math.cos(math.rad(-r)) * 1, z, 0.5) 
    colShapes[col] = obj -- Association of col with obj

    addEventHandler("onColShapeHit", col, function(hitter)
        if getElementType(hitter) == "player" then
            if colShapes[source] then
                outputChatBox("ENTER", hitter, 255, 255, 0, true)
                bindKey(hitter, "F", "down", TakeWeapon, source)
            end
        end 
    end)

    addEventHandler("onColShapeLeave", col, function(hitter)
        if getElementType(hitter) == "player" then
            if colShapes[source] then
                outputChatBox("EXIT", hitter, 255, 255, 0, true)
                unbindKey(hitter, "F", "down", TakeWeapon)
            end
        end 
    end)
end
addEvent("dropKatana", true)
addEventHandler("dropKatana", root, DropWeapon)

function TakeWeapon(player, key, keyState, col)
    local obj = colShapes[col]
    if isElement(obj) then
        giveWeapon(player, 2, 1, true)
        setPedAnimation(player, "BOMBER", "BOM_plant_loop", -1, true, false, false, false)    
        setTimer(setPedAnimation, 1500, 1, player, false)
        unbindKey(player, "F", "down", TakeWeapon)
        -- Destroy the object and its associated collision shape
        destroyElement(obj)
        destroyElement(col)
        colShapes[col] = nil -- Clear the reference to avoid memory leaks
   end
end

 

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