Jump to content

[Help] Firing weapon


ForLaXPy

Recommended Posts

Posted

Well I recently made a new script that gonna attach some weapon to a specific and i bounded this function to key and evrything was going fine the weapons ae attached to the car and its firing and alls good but the issue is when i try to shoot another player car or i try to damage a player , I cannot do well i think because that function is only called for the client when he press  the bind and i was looking arround for something to let that fire dmg the others but i cant find in mta client function wiki that's why I'm seeking for help here 

 

local replacementGunKey = "vehicle_fire" 


function addt()
  bindKey ( replacementGunKey, "both", fireHydraGuns ) 
 end
addEventHandler ( "onClientResourceStart", root, addt)
  

function fireHydraGuns ( key, keyState )
  if ( keyState == "down" ) then
  	if ( key == replacementGunKey ) and hydraGuns == true then
	toggleControl ( replacementGunKey, false )
    setWeaponState(hydraGunL, "firing")
    setWeaponState(hydraGunR, "firing")
	fireWeapon(hydraGunL)
	fireWeapon(hydraGunR)
    setWeaponProperty(hydraGunL, "fire_rotation", 10, 4, 0)
    setWeaponProperty(hydraGunR, "fire_rotation", 10, 4, 0)
	end
  end
  
  if ( keyState == "up" ) then
  	if ( key == replacementGunKey ) or hydraGuns == false then
    setWeaponState(hydraGunL, "ready")
    setWeaponState(hydraGunR, "ready")
	toggleControl ( replacementGunKey, true )
	end
  end 
   end

 

UXW9xy7.png

 

 

  • Moderators
Posted
  • Is the weapon not firing?
  • Or is the weapon not doing damage?

 

It is true that weapons stopped doing damage at a given moment. (not sure what the reason behind this is)

 

You can fix that problem with:

https://wiki.multitheftauto.com/wiki/OnClientWeaponFire

https://wiki.multitheftauto.com/wiki/SetElementHealth

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

the Car is shooting Fine but there is no dmg for vehicles or players I tryed to Add this function using your event and still nothing

 

  
function DamageToPlayersFromCustomWeapons(target)
    if target == localPlayer then
	  setElementHealth ( target, -5 )
	  elseif isElement( target ) and getElementType( target ) == "vehicle" then
	  setElementHealth ( target, -10 )
    end
end
addEventHandler("onClientWeaponFire", root, DamageToPlayersFromCustomWeapons)

 

UXW9xy7.png

 

 

  • Moderators
Posted
function DamageToPlayersFromCustomWeapons(hitElement)
	iprint("onClientWeaponFire")
	if hitElement then
		iprint("hitElement exist")
		if hitElement == localPlayer then 
			setElementHealth(hitElement, math.max(getElementHealth(hitElement) - 5, 0))
			iprint("hit localPlayer")
		elseif getElementType( hitElement ) == "vehicle" then
			setElementHealth(hitElement, math.max(getElementHealth(hitElement) - 10, 0))
			iprint("hit vehicle")
		end
   	end
end
addEventHandler("onClientWeaponFire", root, DamageToPlayersFromCustomWeapons)

 

Try this and show the debug results.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted (edited)

Well @IIYAMA  I tried it and it works for vehicle that in the debug prints :

INFO: "onClientWeaponFire"

INFO: "hitElement exist"

INFO: "hit vehicle"

But when i tested it with a player I found this

INFO: "onClientWeaponFire"

INFO: "hitElement exist" 

and nothing about hit localplayer also no dmg for them

 

Edited by ForLaXPy
fix

UXW9xy7.png

 

 

  • Moderators
Posted

If the localPlayer gets hit, other players will not see: "hit localPlayer" (only the localPlayer will see that message if it is actually working)

 

Replace line 4 with this:

iprint("hitElement exist, element type of the hitElement =", getElementType(hitElement),", the userdata of the hitElement =", hitElement, ", the userdata of the localPlayer =", localPlayer)

To see what is going.

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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