Jump to content

Weapon is not demaging!


Xwad

Recommended Posts

Hi i have a script that attaches a m4 to rhino and allows to shoot with ctrl but when i shoot a player then it dosent demage the player pls help

  
vehWeapons = {} 
function createM4Weapon() 
    if getElementModel(source) == 432 then 
    vehWeapons[source] = {} 
        local vX, vY, vZ = getElementPosition(source) 
        -- Wep 1 
        vehWeapons[source][1] = createWeapon("m4", vX, vY, vZ + 1) 
        setWeaponClipAmmo(vehWeapons[source][1], 500) 
        setWeaponState(vehWeapons[source][1], "ready") 
        attachElements(vehWeapons[source][1],source,0.7,2.4,0.7,0,0,90) 
        setWeaponFiringRate ( vehWeapons[source][1] , 63  ) 
    end 
end 
addEventHandler( "onClientVehicleEnter",getRootElement(),createM4Weapon) 
  
  
addEventHandler("onClientKey", root,  
function(button,state) 
    local veh = getPedOccupiedVehicle(localPlayer) 
    if veh then 
        if getElementModel(veh) == 432 then 
            if vehWeapons[veh][1] then 
                if button == "lctrl" and state == true then 
                    setWeaponState(vehWeapons[veh][1],"firing") 
                else 
                    setWeaponState(vehWeapons[veh][1],"ready") 
                end 
            end 
        end 
    end 
end) 
  
function destroyCurrentVehicleWeapons() 
    destroyElement(vehWeapons[source][1]) 
    vehWeapons[source] = false -- Destroy the Weapons 
end 
addEventHandler("onClientVehicleExit",root, destroyCurrentVehicleWeapons) 
addEventHandler("onClientVehicleExplode",root, destroyCurrentVehicleWeapons) 
  

Link to comment
Hi i have a script that attaches a m4 to rhino and allows to shoot with ctrl but when i shoot a player then it dosent demage the player pls help
  
vehWeapons = {} 
function createM4Weapon() 
    if getElementModel(source) == 432 then 
    vehWeapons[source] = {} 
        local vX, vY, vZ = getElementPosition(source) 
        -- Wep 1 
        vehWeapons[source][1] = createWeapon("m4", vX, vY, vZ + 1) 
        setWeaponClipAmmo(vehWeapons[source][1], 500) 
        setWeaponState(vehWeapons[source][1], "ready") 
        attachElements(vehWeapons[source][1],source,0.7,2.4,0.7,0,0,90) 
        setWeaponFiringRate ( vehWeapons[source][1] , 63  ) 
    end 
end 
addEventHandler( "onClientVehicleEnter",getRootElement(),createM4Weapon) 
  
  
addEventHandler("onClientKey", root,  
function(button,state) 
    local veh = getPedOccupiedVehicle(localPlayer) 
    if veh then 
        if getElementModel(veh) == 432 then 
            if vehWeapons[veh][1] then 
                if button == "lctrl" and state == true then 
                    setWeaponState(vehWeapons[veh][1],"firing") 
                else 
                    setWeaponState(vehWeapons[veh][1],"ready") 
                end 
            end 
        end 
    end 
end) 
  
function destroyCurrentVehicleWeapons() 
    destroyElement(vehWeapons[source][1]) 
    vehWeapons[source] = false -- Destroy the Weapons 
end 
addEventHandler("onClientVehicleExit",root, destroyCurrentVehicleWeapons) 
addEventHandler("onClientVehicleExplode",root, destroyCurrentVehicleWeapons) 
  

Script looks fine, What's the problem.

Link to comment
I think that problem is that weapon is not synced with other players

That's true, he did create the weapon with the event onClientVehicleEnter, the event will trigger for streamed in players only.

Also he used the event onClientKey that trigger only to him self which mean other players won't see the weapon firing.

Link to comment
  • 3 weeks later...
  • 2 weeks later...

I try but i cant make it:/ Is it something like that?

client

  
addEventHandler( "onClientKey", root, function(button,press)  
    if button == "ctrl" then 
        triggerServerEvent ( "createM4Weapon", resourceRoot, "shooting" ) 
        return true 
    end 
    return false 
end ) 
  

server

  
function createM4Weapon ( message ) 
    -- the predefined variable 'client' points to the player who triggered the event and should be used due to security issues    
    triggerClientEvent ( playerSource, "createM4Weapon", playerSource, "Hello World!" ) 
end 
addEvent( "createM4Weapon", true ) 
addEventHandler( "createM4Weapon", resourceRoot, greetingHandler ) -- Bound to this resource only, saves on CPU usage. 
  

Link to comment

you mean need i add this to client?

  
vehWeapons = {} 
function createM4Weapon() 
    if getElementModel(source) == 432 then 
    vehWeapons[source] = {} 
        local vX, vY, vZ = getElementPosition(source) 
        -- Wep 1 
        vehWeapons[source][1] = createWeapon("m4", vX, vY, vZ + 1) 
        setWeaponClipAmmo(vehWeapons[source][1], 500) 
        setWeaponState(vehWeapons[source][1], "ready") 
        attachElements(vehWeapons[source][1],source,0.7,2.4,0.7,0,0,90) 
        setWeaponFiringRate ( vehWeapons[source][1] , 63  ) 
    end 
end 
addEventHandler( "onClientVehicleEnter",getRootElement(),createM4Weapon) 
  
  
addEventHandler("onClientKey", root, 
function(button,state) 
    local veh = getPedOccupiedVehicle(localPlayer) 
    if veh then 
        if getElementModel(veh) == 432 then 
            if vehWeapons[veh][1] then 
                if button == "lctrl" and state == true then 
                    setWeaponState(vehWeapons[veh][1],"firing") 
                else 
                    setWeaponState(vehWeapons[veh][1],"ready") 
                end 
            end 
        end 
    end 
end) 
  
function destroyCurrentVehicleWeapons() 
    destroyElement(vehWeapons[source][1]) 
    vehWeapons[source] = false -- Destroy the Weapons 
end 
addEventHandler("onClientVehicleExit",root, destroyCurrentVehicleWeapons) 
addEventHandler("onClientVehicleExplode",root, destroyCurrentVehicleWeapons) 
  

Link to comment

My script makes possible to attach weapons to vehicles but i cant demage players only vehicles.. And the other problem is that other players cant see the weapon and the projectile and if i press w and start shooting and after that i stop pressing w then the weapon shooting stops.

Link to comment
My script makes possible to attach weapons to vehicles but i cant demage players only vehicles.. And the other problem is that other players cant see the weapon and the projectile and if i press w and start shooting and after that i stop pressing w then the weapon shooting stops.

It's because your script is client side

use triggerClientevent and triggerserver event to fix that.

Link to comment

Fnaly i made i hope i made it good without problems.. So is it good?:D

Client

  
triggerServerEvent ( "MachineGun", localPlayer,  {  
      end  
  
  
  
addEvent ( "MachineGun2", true ); 
addEventHandler ( "MachineGun2", root, function ( createM4Weapon ) 
       
  
vehWeapons = {} 
function createM4Weapon() 
    if getElementModel(source) == 432 then 
    vehWeapons[source] = {} 
        local vX, vY, vZ = getElementPosition(source) 
        -- Wep 1 
        vehWeapons[source][1] = createWeapon("m4", vX, vY, vZ + 1) 
        setWeaponClipAmmo(vehWeapons[source][1], 500) 
        setWeaponState(vehWeapons[source][1], "ready") 
        attachElements(vehWeapons[source][1],source,0.7,2.4,0.7,0,0,90) 
        setWeaponFiringRate ( vehWeapons[source][1] , 63  ) 
    end 
end 
addEventHandler( "onClientVehicleEnter",getRootElement(),createM4Weapon) 
  
  
addEventHandler("onClientKey", root, 
function(button,state) 
    local veh = getPedOccupiedVehicle(localPlayer) 
    if veh then 
        if getElementModel(veh) == 432 then 
            if vehWeapons[veh][1] then 
                if button == "lctrl" and state == true then 
                    setWeaponState(vehWeapons[veh][1],"firing") 
                else 
                    setWeaponState(vehWeapons[veh][1],"ready") 
                end 
            end 
        end 
    end 
end) 
  
function destroyCurrentVehicleWeapons() 
    destroyElement(vehWeapons[source][1]) 
    vehWeapons[source] = false -- Destroy the Weapons 
end 
addEventHandler("onClientVehicleExit",root, destroyCurrentVehicleWeapons) 
addEventHandler("onClientVehicleExplode",root, destroyCurrentVehicleWeapons) 
  

server

  
addEvent ( "MachineGun", true ); 
addEventHandler ( "MachineGun", root, function ( info ) 
       
        triggerClientEvent ( root, "MachineGun2", root, info ); 
         
end ); 
  

Link to comment

I think you should reconsider what you wrote on Line 8 of the client code

addEventHandler ( "MachineGun2", root, function ( createM4Weapon ) 

Didn't you mean

addEventHandler ( "MachineGun2", root, createM4Weapon ) 

— You also need to put it AFTER the createM4Weapon function, not before

And what is that only line 2 and 3?

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