Jump to content

[Help]Destroying Vehicles with deagle


Perfect

Recommended Posts

hi all, i am trying to change deagle to a gun which destroy vehicle which we shoot at. i am trying to change that gun by command "dgun". so when we type dgun, it will change to destroying gun and when we type dgun again it will turn to normal gun.

but unfortunately my code is not working, hope you guys can help me.

here is the code

function Dgun ( attacker, weapon, bodypart, loss)  
    if ( weapon == 24 ) then     
    outputChatBox("Server: D-gun On!",source,0,255,0,true) 
    for i, source in ipairs ( getElementsByType("player") )  
        target = getPlayerTarget ( source )                      
        if ( target ) then                                          
            if ( getElementType ( target ) == "vehicle" ) then      
                destroyElement ( target )                
                outputChatBox("Server: Destroyed!!",source,0,255,0,true) 
            end 
        end 
    end 
    end 
end 
addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) 
addCommandHandler("dgun",Dgun) 

Link to comment
local dgunStatus = false 
  
function Dgun ( attacker, weapon, bodypart, loss) 
if dgunStatus == false then 
dgunStatus = true; 
    if ( weapon == 24 ) then    
    outputChatBox("Server: D-gun On!",source,0,255,0,true) 
    for i, source in ipairs ( getElementsByType("player") ) 
        target = getPlayerTarget ( source )                     
        if ( target ) then                                         
            if ( getElementType ( target ) == "vehicle" ) then     
                destroyElement ( target )               
                outputChatBox("Server: Destroyed!!",source,0,255,0,true) 
            end 
        end 
    end 
    end 
elseif dgunStatus == true then 
dgunStatus = false 
return false; 
end 
addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) 
addCommandHandler("dgun",Dgun) 

Link to comment

its client side or server side ?

and i tried both client and server side. it seems to not working,

i change some things in script as per debugscript says

here is the code.(no errors and no warnings and not working)

function Dgun ( attacker, weapon, bodypart, loss) 
if dgunStatus == false then 
dgunStatus = true; 
    if ( weapon == 24 ) then   
    outputChatBox("Server: D-gun On!",source,0,255,0,true) 
    for i, source in ipairs ( getElementsByType("player") ) 
        do target = getPedTarget ( source )                     
        if ( target ) then                                         
            if ( getElementType ( target ) == "vehicle" ) then     
                destroyElement ( target )               
                outputChatBox("Server: Destroyed!!",source,0,255,0,true) 
            end 
        end 
    end 
    end 
elseif dgunStatus == true then 
dgunStatus = false 
return false; 
end 
end 
addEventHandler ( "onVehicleDamage", getRootElement (), Dgun ) 
addCommandHandler("dgun",Dgun) 

Link to comment

so you mean this will work ?

client

function Dgun ( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) 
if dgunStatus == false then 
dgunStatus = true; 
    if weapon == 24 and getElementType(hitElement)=="vehicle" then 
    outputChatBox("Server: D-gun On!",source,0,255,0,true) 
                destroyElement ( target )               
                outputChatBox("Server: Destroyed!!",source,0,255,0,true) 
            end 
        else 
              outputChatBox("Server: Destroyed!!",source,0,255,0,true) 
    end 
    end 
elseif dgunStatus == true then 
dgunStatus = false 
return false; 
addEventHandler ( "onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) 
addCommandHandler("dgun",Dgun) 

Link to comment

Client Side:

function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) 
    if weapon == 24 and hitElement and dgunStatus then 
        if getElementType(hitElement) == "vehicle" then 
            -- trigger goes here. 
        end 
    end 
end 
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) 
  
addCommandHandler("dgun", 
function() 
    dgunStatus = not dgunStatus 
    outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) 
end) 

Server Side:

-- Your turn to do it. 

Link to comment

ok.

first, thnx for the code.

and i tried my best as what you said. (btw this is my first time using trigger events)

client

function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) 
    if weapon == 24 and hitElement and dgunStatus then 
        if getElementType(hitElement) == "vehicle" then 
          triggerServerEvent ( "devent", localPlayer) 
        end 
    end 
end 
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) 
  
addCommandHandler("dgun", 
function() 
    dgunStatus = not dgunStatus 
    outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) 
end) 

server

function dgevent(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) -- i am confused about this part. i have parameter in client side, so if i am trigger event then i must put those parameters in server side ?. 
destroyVehicle(hitElement) 
end 
addEvent("devent", true) 
addEventHandler("devent", root, dgevent) -- and here what is root,getRootElement() and sound. i mean what will happen if i put one of them ? 

i have got some questions too (see server file), hope you will help again :)

Link to comment
function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) 
    if weapon == 24 and hitElement and dgunStatus then 
        if getElementType(hitElement) == "vehicle" then 
            destroyElement(hitElement) 
        end 
    end 
end 
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) 
  
addCommandHandler("dgun", 
function() 
    dgunStatus = not dgunStatus 
    outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) 
end) 

Link to comment

Client:

function Dgun(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) 
    if weapon == 24 and hitElement and dgunStatus then 
        if getElementType(hitElement) == "vehicle" then 
            triggerServerEvent("onRequestVehicleDestroy", localPlayer, hitElement) 
        end 
    end 
end 
addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), Dgun ) 
  
addCommandHandler("dgun", 
function() 
    dgunStatus = not dgunStatus 
    outputChatBox("Server: D-gun "..(dgunStatus and "On" or "Off").."!", 0, 255, 0, true) 
end) 

Server:

addEvent("onRequestVehicleDestroy", true) 
addEventHandler("onRequestVehicleDestroy", root, 
function(v) 
    if v and isElement(v) then 
        destroyElement(v) 
        outputChatBox("Server: Destroyed!!", source, 0, 255, 0, true) 
    end 
end) 

Link to comment

Guys Thnx so much!!!

@TAPL

Thnx for insisting me to learn how to trigger events.

@Sasuke

Thnx for Giving me the whole and final code.

I really appreciate your help guys and i am gonna analyse that script and going to use in other scripts!

Thank you :D

(and btw @TAPL, i laughed so much when you said "You can't put a :~ in the code and expect it to work.". still laughing when remembering :D )

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