Jump to content

[Help] Head Damage


Lorder

Recommended Posts

--CLIENT

local headshotDamage = 200 -- damage to take when the player is shot in the head

function onPlayerHeadshot( attacker, _, bodypart )
  if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
  if not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player
  
  local playerHealth = getElementHealth( source ) -- get the player's health
  setElementHealth( source, playerHealth - headshotDamage ) -- set the player's new health
  
  cancelEvent() --cancel the event
end 
addEventHandler( "onClientPlayerDamage", getRootElement(), onPlayerHeadshot )

 

Link to comment
28 minutes ago, *BeaT* said:

--CLIENT

local headshotDamage = 200 -- damage to take when the player is shot in the head

function onPlayerHeadshot( attacker, _, bodypart )
  if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
  if not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player
  
  local playerHealth = getElementHealth( source ) -- get the player's health
  setElementHealth( source, playerHealth - headshotDamage ) -- set the player's new health
  
  cancelEvent() --cancel the event
end 
addEventHandler( "onClientPlayerDamage", getRootElement(), onPlayerHeadshot )

 

I'm trying Thx <3 

Link to comment
  • Moderators
function onPlayerHeadshot( attacker, _, bodypart, loss )
local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
local newHealth = math.max(playerHealth - headshotDamage, 0)
setElementHealth( localPlayer, newHealth) -- set the player's new health

 

You have to add the loss back to get the original health. In this case it doesn't really matter since the damage is 200, but if you want to use values lower than 200, you will need to add that.

If you put the health under 0, it will do strange things.(reset etc.)

 

 


addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

You can't set other players their health with success, only that one of the localPlayer. So we only have to check the localPlayer.

 

 


cancelEvent()

No need for cancelling.

 

 

Keep in mind that this can get bugged under strange occasions. Because of the GTA engine.

If this doesn't work, post the updated code.

Link to comment

@IIYAMA

I did it right but I did not work

local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
local newHealth = math.max(playerHealth - headshotDamage, 0)

function onPlayerHeadshot( attacker, _, bodypart, loss )
  if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
  if not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player
  
  local playerHealth = getElementHealth( source ) -- get the player's health
  setElementHealth( localPlayer, newHealth) -- set the player's new health
  
end 
addEventHandler( "onClientPlayerDamage", getRootElement(), onPlayerHeadshot )

 

Link to comment
  • Moderators

Did you? ;)

You should have warnings/errors at the moment, so I don't think so.

 

 

  1. Replace line 8 with line 1 and 2. (of your updated code)
  2. Replace line 12 with the line that looks similar. (from my code)

And show the updated code.

 

 

Link to comment
3 minutes ago, IIYAMA said:

Did you? ;)

You should have warnings/errors at the moment, so I don't think so.

 

 

  1. Replace line 8 with line 1 and 2. (of your updated code)
  2. Replace line 12 with the line that looks similar. (from my code)

And show the updated code.

 

 

Can you help me in this business is new?

Link to comment
 local playerHealth = getElementHealth( source ) -- get the player's health

function onPlayerHeadshot( attacker, _, bodypart, loss )
  if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
  if not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player
  
  local playerHealth = getElementHealth( source ) -- get the player's health
  setElementHealth( localPlayer, newHealth) -- set the player's new health
  
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

Edited by Lorder
Link to comment
  • Moderators
1 minute ago, Lorder said:

How can I find out? Please give me the code 1 I promise you I will try hard to find out

function onPlayerHeadshot( attacker, _, bodypart, loss )
	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

	local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
	local newHealth = math.max(playerOriginalHealth - headshotDamage, 0)
	setElementHealth( localPlayer, newHealth) -- set the player's new health
  
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

Link to comment
  • Moderators
function onPlayerHeadshot( attacker, _, bodypart, loss )
	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not attacker or not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

	local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
	local newHealth = math.max(playerOriginalHealth - headshotDamage, 0)
	setElementHealth( localPlayer, newHealth) -- set the player's new health
  	
  	iprint("playerOriginalHealth:", playerOriginalHealth, ", newHealth:", newHealth)
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

debug it and show results.

Link to comment
  • Moderators
4 minutes ago, Lorder said:

@IIYAMA 5kip5scs.png

 

yea, that explains a lot.

Run the script clientside, not serverside.

 

  1. Make a backup of the meta.xml (so can mess it up)
  2. Open the meta.xml
  3. <script src="scriptName.lua" /> change to: <script src="scriptName.lua" type="client"/>

  4. Restart the resource.

Edited by IIYAMA
Link to comment

@IIYAMAit does not work

meta.xml

<meta>
	<script src="hs.lua" type="client"/>
</meta>

hs.lua

function onPlayerHeadshot( attacker, _, bodypart, loss )
	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not attacker or not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

	local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
	local newHealth = math.max(playerOriginalHealth - headshotDamage, 90)
	setElementHealth( localPlayer, newHealth) -- set the player's new health
  	
  	iprint("playerOriginalHealth:", playerOriginalHealth, ", newHealth:", newHealth)
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

Edited by Lorder
Link to comment
13 minutes ago, Lorder said:

@IIYAMAit does not work

meta.xml


<meta>
	<script src="hs.lua" type="client"/>
</meta>

hs.lua


function onPlayerHeadshot( attacker, _, bodypart, loss )
	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not attacker or not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

	local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
	local newHealth = math.max(playerOriginalHealth - headshotDamage, 90)
	setElementHealth( localPlayer, newHealth) -- set the player's new health
  	
  	iprint("playerOriginalHealth:", playerOriginalHealth, ", newHealth:", newHealth)
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

 

Show the debug again

Link to comment
  • Moderators

You edited line 7, that is a bit sloppy of you. If you put that to 90, the health can never be lower than 90.

If you want it to work, change it back to the code I gave you.

 

My code:

local newHealth = math.max(playerOriginalHealth - headshotDamage, 0)

 

Your code:

local newHealth = math.max(playerOriginalHealth - headshotDamage, 90)

Link to comment
13 minutes ago, IIYAMA said:

You edited line 7, that is a bit sloppy of you. If you put that to 90, the health can never be lower than 90.

If you want it to work, change it back to the code I gave you.

 

My code:

local newHealth = math.max(playerOriginalHealth - headshotDamage, 0)

 

Your code:

local newHealth = math.max(playerOriginalHealth - headshotDamage, 90)

I tried, but it didn't work, no bugs

Link to comment
  • Moderators

 

 

I did test the code without this part:

	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not attacker or not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

Just to check if it is working (because I didn't have a dummy to test it on). It did work.

 

 

If you disable that, will it give you an insta-kill when you receive any damage?

If not, then you have :Oed it up somewhere along the way.

 

Link to comment
18 minutes ago, IIYAMA said:

 

 

I did test the code without this part:


	if not ( bodypart == 9 ) then return false end -- check if the player has been hit in the head
	if not attacker or not ( getElementType( attacker ) == "player" ) then return false end -- check if the attacker is a player

Just to check if it is working (because I didn't have a dummy to test it on). It did work.

 

 

If you disable that, will it give you an insta-kill when you receive any damage?

If not, then you have :Oed it up somewhere along the way.

 

I try, but he does not get much damage from his head

function onPlayerHeadshot( attacker, _, bodypart, loss )

	local playerOriginalHealth = getElementHealth( source ) + loss -- get the player's original health
	local newHealth = math.max(playerOriginalHealth - headshotDamage, 0)
	setElementHealth( localPlayer, newHealth) -- set the player's new health
  	
  	iprint("playerOriginalHealth:", playerOriginalHealth, ", newHealth:", newHealth)
end 
addEventHandler( "onClientPlayerDamage", localPlayer, onPlayerHeadshot )

 

Edited by Lorder
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...