Jump to content

I need help for an simple script


Hero192

Recommended Posts

Hello there,i want to release an script but to start on it i need to know this what im gonna tell you

I want when the player fire the weapon ID == 17 the element be destroyed

I tried that but failed,please give me the functions / events what should i use,thanks in advance

I tried that but nothing happened :P anyone can help me for that ?

--Client side:

addEventHandler("onWeaponFire", root, 
function( ) 
for _, p in ipairs ( getElementsByType ( "player" ) ) do 
local tear = getPedWeapon(p) 
   if (tear == 17)  then 
    triggerServerEvent ("takeweapon",getLocalPlayer()) 
           end 
      end 
 end 
      ) 

--Server side:

addEvent( "takeweapon", true ) 
addEventHandler("takeweapon", g_Root, 
 function() 
   setTimer ( takeWeapon( tear),1000,1) 
  end 
) 

Edited by Guest
Link to comment

I tried that but nothing happened :P anyone can help me for that ?

--Client side:

addEventHandler("onWeaponFire", root, 
function( ) 
for _, p in ipairs ( getElementsByType ( "player" ) ) do 
local tear = getPedWeapon(p) 
   if (tear == 17)  then 
    triggerServerEvent ("takeweapon",getLocalPlayer()) 
            end 
      end 
end 
      ) 

--Server side:

addEvent( "takeweapon", true ) 
addEventHandler("takeweapon", g_Root, 
 function() 
   setTimer ( takeWeapon( tear),1000,1) 
  end 
) 

Link to comment

You're using server-side event in client-side part.

Server:

addEventHandler("onWeaponFire", root, 
    function() 
        local tear = getPedWeapon(source) 
        if tear == 17 then 
            cancelEvent() 
            takeWeapon(tear) 
        end 
    end 
) 

Link to comment
You're using server-side event in client-side part.

Server:

addEventHandler("onWeaponFire", root, 
    function() 
        local tear = getPedWeapon(source) 
        if tear == 17 then 
            cancelEvent() 
            takeWeapon(tear) 
        end 
    end 
) 

You need to specify the player element when running it server-side.

addEventHandler("onWeaponFire", root, 
    function() 
        local tear = getPedWeapon(source) 
        if tear == 17 then 
            cancelEvent() 
            takeWeapon(source, tear) 
        end 
    end 
) 

Link to comment

Whichever would work, but if you were to put it client-side you'd have to trigger an event, which seems rather unnecessary when you could just put the timer server-side like this;

addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() 
            setTimer(function() 
                takeWeapon(source, currentWeaponID) 
            end, 1000, 1) 
        end 
    end 
) 

Link to comment
Whichever would work, but if you were to put it client-side you'd have to trigger an event, which seems rather unnecessary when you could just put the timer server-side like this;
addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() 
            setTimer(function() 
                takeWeapon(source, currentWeaponID) 
            end, 1000, 1) 
        end 
    end 
) 

You forgot to pass source to function inside setTimer.

addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() 
            setTimer(function(source) 
                takeWeapon(source, currentWeaponID) 
            end, 1000, 1, source) 
        end 
    end 
) 

Or:

addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() -- You can also remove this. 
            setTimer(takeWeapon, 1000, 1, source, currentWeaponID) 
        end 
    end 
) 

Link to comment

onWeaponFire is not triggered for non-custom weapons. You need to implement your own sender event client-side, which sends the stuff to the server. Make sure to call the server trigger only once, or otherwise you can get hundreds of requests per second, which is not necessarily the thing you want. You can use a timer to do that properly, client-side.

You can use onClientPlayerWeaponFire, which you then bind to the localPlayer (instead of triggering for any streamed in players that fires their weapon).

Link to comment

You forgot to pass source to function inside setTimer.

addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() 
            setTimer(function(source) 
                takeWeapon(source, currentWeaponID) 
            end, 1000, 1, source) 
        end 
    end 
) 

Or:

addEventHandler("onWeaponFire", root, 
    function() 
        local currentWeaponID = getPedWeapon(source) 
        if(currentWeaponID == 17) then 
            cancelEvent() -- You can also remove this. 
            setTimer(takeWeapon, 1000, 1, source, currentWeaponID) 
        end 
    end 
) 

You actually don't need to pass them, so long as the variable is defined before the timer. There are some cases where source won't work, because it will be "discarded" after a few milliseconds. I believe onPlayerLogin does this. :)

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