Jump to content

Damages duplicating?


Recommended Posts

Hi why is the damage being duplicated whenever there are more than one players in the server? if i'm testing by myself it works fine, but whenever there are like 3-4 players, this goes glitched and the elementdatas/ damage counts are duplicating.

it only happens if there are more than 1 player in the server and it's weird.

Client:

function botdamaged ( attacker, weapon, bodypart )
	if getElementType ( source ) == "ped" then
		local bot = getElementData(source, "bot")
		local settlementguard = getElementData(source, "settlementguard")
		local boss = getElementData(source, "boss")
		local zombie = getElementData(source, "zombie")
		local zombieSpecial = getElementData(source, "zombieSpecial")
		if boss then 
			triggerServerEvent ("bossdamages", source, source, attacker, weapon, bodypart )
			cancelEvent()
		elseif settlementguard then 
			if attacker then 
				if getElementType(attacker) == "player" then 
				local playerTeam = getPlayerTeam(attacker)
				local teamName = getTeamName(playerTeam)
				local teamData = getElementData(source, "settlementguard") 
					if teamName == teamData then 
						exports.amhelp:helpmessage("You cannot kill friendlies!", 255, 0, 0)
						cancelEvent()
					else
						triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
						cancelEvent()
					end 
				else 
					triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
					cancelEvent()
				end 
			end 
		elseif bot or zombieSpecial then 
			triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
			cancelEvent()
		elseif zombie then
			if attacker and getElementType(attacker) == "ped" then 
				triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
			end 
		end 
		if boss == "Mutant" then 
			if attacker and attacker == localPlayer then 
				if isTimer(participatetimer) then 
					killTimer(participatetimer) 
					setElementData(attacker, "bossparticipated", true) 
				end 
				participatetimer = setTimer(function()
					setElementData(attacker, "bossparticipated", false)
				end, 240000, 1)
			end 
		end 
	end
end 
addEventHandler ( "onClientPedDamage", root, botdamaged)

 

Server:

local shotguns = { [27] = true, [25] = true, [26] = true }

local assaultrifles = { [31] = true, [30] = true } 

local lightweapons = { [29] = true, [32] = true, [28] = true, [23] = true, [22] = true }

local snipers = { [33] = true, [34] = true }

local heavybullets = { [24] = true, [38] = true }

addEvent( "botdamages", true )
function botdamages( ped,attacker, weapon, bodypart)
local bot = getElementData(ped, "bot")
local zombie = getElementData(ped, "zombie")
local zombieSpecial = getElementData(ped, "zombieSpecial")
local health = getElementData(ped, "elementHealth")
	if bot or zombieSpecial then 
		if attacker and getElementType(attacker) == "ped" then 
			if getElementData(attacker, "zombie") == true then 
				setElementData(ped, "elementHealth", tonumber(health) - math.random(50,60))
			else 
				setElementData(ped, "elementHealth", tonumber(health) - math.random(30,45))
			end 
			if getElementData(ped, "elementHealth") < 20 then
				killPed(ped, attacker, weapon, bodypart ) 
			end 
		else 
			if shotguns[weapon] then 
				setElementData(ped, "elementHealth", tonumber(health) - math.random(50,60))
			else
				if assaultrifles[weapon] then 
					setElementData(ped, "elementHealth", tonumber(health) - math.random(30,45))
				elseif lightweapons[weapon] then 
					setElementData(ped, "elementHealth", tonumber(health) - math.random(20,25))
				elseif snipers[weapon] then 
					setElementData(ped, "elementHealth", tonumber(health) - math.random(250,300))
				elseif heavybullets[weapon] then 
					setElementData(ped, "elementHealth", tonumber(health) - math.random(70,100))
				end 
			end
			if getElementData(ped, "elementHealth") < 20 then
				killPed(ped, attacker, weapon, bodypart ) 
			end 
		end 
	elseif zombie then 
		if attacker and getElementType(attacker) == "ped" then 
			local health = getElementHealth(ped)
			setElementHealth(ped, health-20)
			if health < 30 then 
				killPed(ped, attacker, weapon, bodypart ) 
			end 
		end 
	end 
end 
addEventHandler( "botdamages", root, botdamages )

 

Edited by Bean666
Link to post
  • Moderators
1 hour ago, Bean666 said:

Hi why is the damage being duplicated whenever there are more than one players in the server?

 

Here is your multiplier:

 

 

991px-Server-based-network.svg.png

Each pc/player is running a copy of this file:

function botdamaged ( attacker, weapon, bodypart )
	if getElementType ( source ) == "ped" then
		local bot = getElementData(source, "bot")
		local settlementguard = getElementData(source, "settlementguard")
		local boss = getElementData(source, "boss")
		local zombie = getElementData(source, "zombie")
		local zombieSpecial = getElementData(source, "zombieSpecial")
		if boss then 
--[[ ... ]]

 

This file will check if ANY ped is damaged:

addEventHandler ( "onClientPedDamage", root, botdamaged)

The event "onClientPedDamage" does not only trigger when <you> damage the ped. It triggers always when a ped is damaged. (If it happens in the game of player, this can differ when the ping is higher/lower)

 


 

When that happens, each player will do this:

triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )

And this (serverside):

addEventHandler( "botdamages", root, botdamages )

 


 

To solve the issue, you have to check if the attacker is <you>.

if attacker == localPlayer then
  
end

 

 

 

Link to post
11 minutes ago, IIYAMA said:

To solve the issue, you have to check if the attacker is <you>.


if attacker == localPlayer then
  
end

 

This has the advantage that the damage event will necessarily be triggered if the attacker sees himself hitting the ped. However, the attacker is not necessarily a player, and the server side code indicates that peds are supposed to damage each other too. Therefore, this code prevents the event from triggering in cases where a ped damages another ped.

It's better to check if you are the syncer of the ped:

if isElementSyncer(ped) then

end

This way, the ped will be damaged if the player who syncs the ped sees him getting damaged. It's also more consistent this way because that's exactly what the syncer is for: controlling what happens to the synced element.

Link to post
Posted (edited)
33 minutes ago, IIYAMA said:

 

Here is your multiplier:

 

 

991px-Server-based-network.svg.png

Each pc/player is running a copy of this file:



function botdamaged ( attacker, weapon, bodypart )
	if getElementType ( source ) == "ped" then
		local bot = getElementData(source, "bot")
		local settlementguard = getElementData(source, "settlementguard")
		local boss = getElementData(source, "boss")
		local zombie = getElementData(source, "zombie")
		local zombieSpecial = getElementData(source, "zombieSpecial")
		if boss then 
--[[ ... ]]

 

This file will check if ANY ped is damaged:



addEventHandler ( "onClientPedDamage", root, botdamaged)

The event "onClientPedDamage" does not only trigger when <you> damage the ped. It triggers always when a ped is damaged. (If it happens in the game of player, this can differ when the ping is higher/lower)

 


 

When that happens, each player will do this:



triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )

And this (serverside):



addEventHandler( "botdamages", root, botdamages )

 


 

To solve the issue, you have to check if the attacker is <you>.



if attacker == localPlayer then
  
end

 

 

 

function botdamaged ( attacker, weapon, bodypart )
	if getElementType ( source ) == "ped" then
		local bot = getElementData(source, "bot")
		local settlementguard = getElementData(source, "settlementguard")
		local boss = getElementData(source, "boss")
		local zombie = getElementData(source, "zombie")
		local zombieSpecial = getElementData(source, "zombieSpecial")
		if attacker == localPlayer then 
			if boss then 
				triggerServerEvent ("bossdamages", source, source, attacker, weapon, bodypart )
				cancelEvent()
			elseif settlementguard then 
				local playerTeam = getPlayerTeam(attacker)
				local teamName = getTeamName(playerTeam)
				local teamData = getElementData(source, "settlementguard") 
				if teamName == teamData then 
					exports.amhelp:helpmessage("You cannot kill friendlies!", 255, 0, 0)
					cancelEvent()
				else
					triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
					cancelEvent()
				end 
			elseif bot or zombieSpecial then 
				triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
				cancelEvent()
			end 
			if boss == "Mutant" then 
				if attacker and attacker == localPlayer then 
					if isTimer(participatetimer) then 
						killTimer(participatetimer) 
						setElementData(attacker, "bossparticipated", true) 
					end 
					participatetimer = setTimer(function()
						setElementData(attacker, "bossparticipated", false)
					end, 240000, 1)
				end 
			end 
		elseif attacker and getElementType(attacker) == "ped" then 
			if zombie then --- source
				triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
			elseif bot then 
				triggerServerEvent ("botdamages", source, source, attacker, weapon, bodypart )
			end 
		end
	end 
end 
addEventHandler ( "onClientPedDamage", root, botdamaged)

I added it now, i also added an elseif if the attacker is a ped, would that be fine?

Edited by Bean666
Link to post
Posted (edited)
5 minutes ago, CrystalMV said:

The damage will still be triggered multiple times if multiple players see the ped damaging another ped.

as long as it does not affect player damages it would be fine for me.

But the player damages wont duplicate now right? only for the peds, since there's no really local ped thingy

Edited by Bean666
Link to post

Player damages won't duplicate, right, but you probably didn't notice my first reply. If you use isElementSyncer instead of checking "attacker == localPlayer", the damage will be synced by the player who syncs the ped, and there will be no duplications whether the attacker is a player or a ped.

Link to post
Posted (edited)
35 minutes ago, CrystalMV said:

This has the advantage that the damage event will necessarily be triggered if the attacker sees himself hitting the ped. However, the attacker is not necessarily a player, and the server side code indicates that peds are supposed to damage each other too. Therefore, this code prevents the event from triggering in cases where a ped damages another ped.

It's better to check if you are the syncer of the ped:



if isElementSyncer(ped) then

end

This way, the ped will be damaged if the player who syncs the ped sees him getting damaged. It's also more consistent this way because that's exactly what the syncer is for: controlling what happens to the synced element.

so basically i just replace the 

if attacker == localPlayer to this?

says element at arg 1 nil.

Edited by Bean666
Link to post

My mistake, I meant

if isElementSyncer(source) then

But now I looked at your code more closely and realized that this checking will prevent the code inside "if boss == "Mutant" then" block from working when the attacker is not the syncer, because that code requires the attacker to be the local player. So that would require more changes. Or you can just keep using "attacker == localPlayer" instead of isElementSyncer after all.

  • Like 1
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...