Jump to content

help


Hrayed

Recommended Posts

Guy I need help with script of drop ammo

I'd like to make it /dropweapon and it drops 50 ammo of the current weapon

Client:

function createPickup (player) 
    local px, py, pz = getElementPosition ( player ) 
    local currentweapon = getPlayerWeapon ( player ) 
    if (currentweapon) then 
        drop = createPickup ( px, py, pz, 2, currentweapon, 3000, 50) 
    end 
end 
addCommandHandler ( "dropweapon", createPickup) 

server:

function onPickupHitFunc ( thePlayer )                 
    if getPickupType ( drop ) == 2 then               
        local ammo = getPickupAmmo ( drop )           
        if ammo < 50 then                               
            local weapon = getPickupWeapon ( drop )   
            giveWeaponAmmo ( thePlayer, weapon, 50 )   
            outputChatBox ( "You just picked up a " .. getWeaponNameFromID(weapon) .. " with " .. ammo .. " ammo", thePlayer ) 
        end 
    end 
end 
addEventHandler ( "onPickupHit", getRootElement(), onPickupHitFunc ) 

Need help :)

Link to comment

You've named your function as the same as a native MTA function, that'll cause a stack overflow.

function createWeaponPickup ( player ) 
    local px, py, pz = getElementPosition ( player ) 
    local currentweapon = getPlayerWeapon ( player ) 
    if ( currentweapon ) then 
        drop = createPickup ( px, py, pz, 2, currentweapon, 3000, 50 ) 
    end 
end 
addCommandHandler ( "dropweapon", createWeaponPickup ) 

Link to comment
You've named your function as the same as a native MTA function, that'll cause a stack overflow.

It's recursion. http://en.wikipedia.org/wiki/Recursion

Errors:

dropweapon.lua:2: Bad argument @ 'getElementPosition'

dropweapon.lua:3: Bad argument @ 'getPedWeapon'

Because player is string (cmd). Command handler function in client side not have player argument. Only server side! So use predefined variable localPlayer.

Also use

getPedWeapon 

instead of

getPlayerWeapon 

because getPlayerWeapon function is outdated.

function CreateWeaponPickup ( ) 
    local fX, fY, fZ = getElementPosition ( localPlayer ) 
    local nWeapon = getPedWeapon ( localPlayer ) 
    if nWeapon then 
        drop = createPickup ( fX, fY, fZ, 2, nWeapon, 3000, 50 ) 
    end 
end 
addCommandHandler ( "dropweapon", CreateWeaponPickup ) 

Link to comment
But the problem is that I can't take it back after when it is dropped.

You should use onClientPickupHit but we should trigger to server side event for give player weapon and use some manipulations. So better make all in server side. Also it's sync for all players. I think you needed this.

local pPickup 
  
function CreateWeaponPickup( pPlayer, _, sAmmo ) 
    local fX, fY, fZ = getElementPosition ( pPlayer ) 
    local nWeapon = getPedWeapon( pPlayer ) 
    if sAmmo then 
        takeWeapon( pPlayer, nWeapon, tonumber( sAmmo ) ) 
    end 
    if nWeapon then 
        pPickup = createPickup( fX, fY, fZ, 2, nWeapon, 3000, sAmmo and tonumber( sAmmo ) or 50 ) 
        setElementData( pPickup, 'custom_pickup', true ) 
    end 
end 
  
function OnPickupHitFunc( pPlayer ) 
    if getElementData( source, 'custom_pickup' ) and getPickupType ( pPickup ) == 2 then               
        local nAmmo = getPickupAmmo( pPickup )                          
        local nWeapon = getPickupWeapon( pPickup ) 
        giveWeapon( pPlayer, nWeapon, nAmmo, true )   
        outputChatBox( 'You just picked up a ' .. getWeaponNameFromID( nWeapon ) .. ' with ' .. nAmmo .. ' ammo', pPlayer ) 
    end 
end 
  
addCommandHandler ( 'dropweapon', CreateWeaponPickup ) 
addEventHandler ( 'onPickupHit', root, OnPickupHitFunc ) 

Updated.

Edited by Guest
Link to comment

Works, but when I type /dropweapon 10 I lose 10 ammo, and when I pick up them again I win 50 ammo; and when I type /dropweapon it drop 50 ammo but I don't lose anything. Also I don't want that the dropped weapon spawn again after when someone take it. :wink:

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