Sign in to follow this  
Xwad

Weapon is not demaging!

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) 
  

Share this post


Link to post
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.

Share this post


Link to post

No i using this to my WW2 server. The problem is that i cant demage players when i shoot with the attached m4

Share this post


Link to post
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.

Share this post


Link to post

You mean need i trigger the client event for setelementHealth?

Share this post


Link to post

No, onClientkey > triggerserverevent >triggerclientevent > making the weapin fire

Share this post


Link to post

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. 
  

Share this post


Link to post

playerSource it's not defined in your code replace it with client.

Share this post


Link to post

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) 
  

Share this post


Link to post

is there any video where they teach how to define player source? becaouse i dont know how:/

Share this post


Link to post
is there any video where they teach how to define player source? becaouse i dont know how:/

what do you mean by player source , can you explane to us what you want exactly.

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post

ok but what functions or events need i add to server side?

Share this post


Link to post

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 ); 
  

Share this post


Link to post

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?

Share this post


Link to post

the line 2 and 3 is for triggering the server event. Is it not good?

Share this post


Link to post

is it now correct?

  
triggerServerEvent ( "MachineGun", root, localPlayer,  { 
      end 
  

Share this post


Link to post

or need i give for it and event for onCloentWeaponFire?

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.