Jump to content

[HELP] vehicle weapon


iFerasMO

Recommended Posts

there's > createWeapon & attachElements

i dont want to change this functions

just i want it work when i Enter vehicle ID 600

-- Client -- 
  
local weapon = {sound = {}} 
  
addEvent("onCreateWeaponVehicle", true) 
addEventHandler("onCreateWeaponVehicle", root, 
function (weaponID) 
    local theVehicle = source 
    if not weaponID then weaponID= 38 end 
    if (getElementType(theVehicle) == "vehicle") and not (weapon[theVehicle]) then 
        local x, y, z = getElementPosition(theVehicle) 
        weapon[theVehicle] = createWeapon(weaponID, x, y, z) 
        attachElements(weapon[theVehicle], theVehicle, -0.13, -0.54, 1.03, 0, 0, 90) 
        setElementData(theVehicle, "VehicleMG", weapon[theVehicle]) 
        setElementAlpha(weapon[theVehicle], 0)  
        setWeaponClipAmmo(weapon[theVehicle], 99999) 
    end 
end) 
  
  
addEvent("onRemoveWeaponVehicle", true) 
addEventHandler("onRemoveWeaponVehicle", root, 
function ( ) 
    local theVehicle = source 
    if (getElementType(theVehicle) == "vehicle") and (weapon[theVehicle]) then 
        destroyElement (weapon[theVehicle]) 
    end 
end) 
addEvent("onWeaponVehicleFiring", true) 
addEventHandler("onWeaponVehicleFiring", root, 
function( ) 
    local theVehicle = source 
    if (theVehicle) and (weapon[theVehicle]) then 
        setWeaponState(weapon[theVehicle], "firing") 
       local x, y, z = getElementPosition(theVehicle) 
        local xm, ym, zm = getElementPosition(weapon[theVehicle]) 
        if not (weapon.sound[theVehicle]) then 
            weapon.sound[theVehicle] = playSound3D("minigun.wav", xm, ym, zm, true) 
            setSoundMaxDistance(weapon.sound[theVehicle], 100) 
            setSoundEffectEnabled(weapon.sound[theVehicle], "gargle", true) 
            attachElements(weapon.sound[theVehicle], weapon[theVehicle]) 
        end 
    end 
end) 
  
addEvent("onWeaponVehicleReady", true) 
addEventHandler("onWeaponVehicleReady", root, 
function ( ) 
    local theVehicle = source 
    if (theVehicle) and (weapon[theVehicle]) then 
        setWeaponState(weapon[theVehicle], "ready") 
        if (weapon.sound[theVehicle]) then 
            stopSound(weapon.sound[theVehicle]) 
            weapon.sound[theVehicle] = nil 
        end 
    end 
end) 
  
bindKey("r", "up", 
function( ) 
    local theVehicle = getPedOccupiedVehicle(localPlayer) 
    if (theVehicle) and (getVehicleController(theVehicle) == thePlayer) then 
        setWeaponState(weapon[theVehicle], "reloading") 
    end 
end) 
  
addEventHandler("onClientWeaponFire", getRootElement(), 
function (element) 
    local theVehicle = getPedOccupiedVehicle(localPlayer) 
    if (theVehicle) and (weapon[theVehicle]) then 
        if (element == theVehicle) then 
            cancelEvent() 
        end 
    end 
end) 
  
addEventHandler("onElementDestroy", root, 
function ( ) 
    local theVehicle = source 
    if (getElementType(theVehicle) == "vehicle") and (weapon[theVehicle]) then 
         destroyElement (weapon[theVehicle]) 
         weapon[theVehicle] = nil 
         setElementData(theVehicle, "VehicleMG", false) 
    end 
end) 

-- Server -- 
  
function onEnter(thePlayer) 
    local theVehicle = getPedOccupiedVehicle(thePlayer) 
    if ( getElementModel ( theVehicle ) == 600 ) then 
    if (getVehicleController(theVehicle) == thePlayer) then 
        triggerClientEvent("onCreateWeaponVehicle", theVehicle, id) 
      end 
   end 
end 
addEventHandler ( "onVehicleEnter", getRootElement(), onEnter ) 
  
function onExit(thePlayer) 
     if ( getElementModel ( source  ) == 600 ) then 
         triggerClientEvent("onRemoveWeaponVehicle", source ) 
    end 
end 
addEventHandler ( "onVehicleExit", getRootElement(), onExit ) 
  
addEventHandler("onResourceStart", root, 
function( ) 
    for i, player in ipairs(getElementsByType("player")) do 
        bindKey(player, "vehicle_secondary_fire", "down", keyFire) 
        bindKey(player, "vehicle_secondary_fire", "up", keyStopFire) 
    end 
end) 
  
function keyFire(source) 
    local theVehicle = getPedOccupiedVehicle(source) 
    if (theVehicle) and (getVehicleController(theVehicle) == source) then 
        triggerClientEvent("onWeaponVehicleFiring", theVehicle) 
    end 
end 
  
function keyStopFire(source) 
    local theVehicle = getPedOccupiedVehicle(source) 
    if (theVehicle) and (getVehicleController(theVehicle) == source) then 
        triggerClientEvent("onWeaponVehicleReady", theVehicle) 
    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...