Galactix Posted June 12, 2018 Share Posted June 12, 2018 Hello, I am using a zombie script and I started using a damage multiplier resource that uses the setElementHealth function. The problem is that now my zombies do not give money at death anymore as the game doesn't consider that a player killed the ped. I'm using this code to reward money at zombie death: addEvent("onZombieWasted",true) addEventHandler("onZombieWasted",root, function (attacker) givePlayerMoney(attacker,math.random(75,150)) -- default 50$ end) Is there any kind of variable or function that would allow me to give money to the last person that hit the zombie before it dies? Link to comment
AJXB Posted June 12, 2018 Share Posted June 12, 2018 How about you show us how are you triggering that event? Link to comment
Galactix Posted June 12, 2018 Author Share Posted June 12, 2018 function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then local oldZcount = getElementData ( attacker, "Zombie kills" ) if oldZcount ~= false then setElementData ( attacker, "Zombie kills", oldZcount+1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) else setElementData ( attacker, "Zombie kills", 1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) end end end end end addEventHandler("onPedWasted", resourceRoot, deanimated) This function is the one that trigger the event Link to comment
AJXB Posted June 12, 2018 Share Posted June 12, 2018 You can just make a function rather than making an event since they're both on the server side. function onZombieWasted (attacker) if (isElement( attacker )) then givePlayerMoney(attacker,math.random(75,150)) -- default 50$ end end function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then local oldZcount = getElementData ( attacker, "Zombie kills" ) if oldZcount ~= false then setElementData ( attacker, "Zombie kills", oldZcount+1 ) else setElementData ( attacker, "Zombie kills", 1 ) end onZombieWasted ( attacker ) end end end end addEventHandler("onPedWasted", resourceRoot, deanimated) You might wanna add a check to the weapon used, to avoid abuse Link to comment
Galactix Posted June 12, 2018 Author Share Posted June 12, 2018 It does like before: sometimes it gives money when the damage dealt isn't too high, but with headshots it just doesn't give any money because the damage is too high. Here's the code I'm using for the dmg multiplier local weaponTable = { -- populate this list by adding weapons: -- [weap_id] = { torso, ass, left_arm, right_arm, left_leg, right_leg, head } [22] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [23] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [24] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [25] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [26] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [27] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [28] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [29] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [30] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [31] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [32] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [33] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5}, [34] = { 1.25, 0.75, 0.75, 0.75, 0.75, 0.75, 1.5} } local weaponDmg = { [22] = {25}, --colt [23] = {40}, --silenced [24] = {70}, --deagle [25] = {10}, --shotgun [26] = {10}, --sawed-off [27] = {15}, --spaz [28] = {20}, --uzi [29] = {25}, --mp5 [30] = {30}, --ak47 [31] = {30}, --m4 [32] = {20}, --tec9 [33] = {75}, --rifle [34] = {125} --sniper rifle } addEvent( "onZombieDamage", true ) function Zheadhit ( ped,attacker, weapon, bodypart) if (getElementData (ped, "zombie") == true) then local bodyPartsDamageTable = weaponTable[ getPedWeapon (attacker) ] local weaponDmgTable = weaponDmg [ getPedWeapon (attacker)] if getElementType(attacker) == "player" and bodyPartsDamageTable then local damageMultiplier = bodyPartsDamageTable [ bodypart - 2] setElementHealth ( ped, getElementHealth(ped) - (damageMultiplier * weaponDmgTable [1] ) ) end end end addEventHandler( "onZombieDamage", getRootElement(), Zheadhit ) Link to comment
AJXB Posted June 12, 2018 Share Posted June 12, 2018 That function clearly triggers addEventHandler("onPedWasted", resourceRoot, deanimated) onPedWasted, the Wasted part means the ped is dead, thus, it triggers when the Zombie is dead. You should make a different event/function onPedDamage Link to comment
Galactix Posted June 12, 2018 Author Share Posted June 12, 2018 Yeah I thought about that but how would I make it so that it rewards the player only when the zombie dies and not just when it gets hit? Link to comment
AJXB Posted June 12, 2018 Share Posted June 12, 2018 I already gave you the code, read above please, and understand the code. Link to comment
Saml1er Posted June 12, 2018 Share Posted June 12, 2018 (edited) Your code looks fine to me, how about you test this code: addEvent("onZombieWasted",true) addEventHandler("onZombieWasted",root, function (attacker) outputChatBox ("onZombieWasted event triggered, attacker is: "..getPlayerName(attacker)) givePlayerMoney(attacker,math.random(75,150)) -- default 50$ end) function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then local oldZcount = getElementData ( attacker, "Zombie kills" ) if oldZcount ~= false then setElementData ( attacker, "Zombie kills", oldZcount+1 ) else setElementData ( attacker, "Zombie kills", 1 ) end outputChatBox ("Triggering onZombieWasted event now") triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) end end else outputChatBox ("Ped died without an attacker") end end addEventHandler("onPedWasted", resourceRoot, deanimated) When you kill a zombie, does it say "Ped died without an attacker..."? Also, you might want to check SP3CTR3's code. Edited June 12, 2018 by Saml1er 1 Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 Exact same problem... I understood your code SP3CTR3 and tried to use it again for the second time, but it only counts less than half the kills I do, and same goes for your script Saml1er, it doesn't do it on every kill, but it does say the message when kills are counted. Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 UPDATE: It seems that somehow the zombies that aren't considered killed by the player and which don't give any reward seem "alive", like their corpse keeps rotating in the player direction, but not the ones that do reward the player. Link to comment
AJXB Posted June 14, 2018 Share Posted June 14, 2018 First you wanted to trigger the event onWasted, then onDamage, now you're asking onWasted again. What. Do. You. Want? Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 All I want is the zombie to reward the player when it dies, I don't know nor care what event I'm using as long as it does what I want it to do. I've been trying to store values in the ped itself when it gets damaged so that it saves the player name when it gets hit so that when it dies it rewards the last player that hit the zombie gets the reward but I wasn't successful, so at this point I really don't know what to do... Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 Thank you for your help everyone. After hours spent trying to get this working I finally came up with a solution using a condition that checks if the damage dealt by the shot is higher than the remaining zombie health, it instantly kills it using killPed and rewards the player, which solved the problem of the damage dealt by setElementHealth being too high and actually making the script unable to detect any attacker. Link to comment
Saml1er Posted June 14, 2018 Share Posted June 14, 2018 (edited) 5 minutes ago, Galactix said: Thank you for your help everyone. After hours spent trying to get this working I finally came up with a solution using a condition that checks if the damage dealt by the shot is higher than the remaining zombie health, it instantly kills it using killPed and rewards the player, which solved the problem of the damage dealt by setElementHealth being too high and actually making the script unable to detect any attacker. addEventHandler("onPedWasted", resourceRoot, deanimated) Why do you have resourceRoot in there? since you put resourceRoot in there, it means if a zombie is created by any other resource, that kill won't be counted. To fix this, just replace resourceRoot with root, like this: addEventHandler("onPedWasted", root, deanimated) I understand, you were able to fix this problem by checking damage, but that's not something you should rely on. onPedWasted is the right event for this. Edited June 14, 2018 by Saml1er Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 Just now, Saml1er said: addEventHandler("onPedWasted", resourceRoot, deanimated) Why do you have resourceRoot in there? since you put resourceRoot in there, it means if a zombie is created by any other resource, that kill won't be counted. To fix this, just replace resourceRoot with root, like this: addEventHandler("onPedWasted", root, deanimated) Welp, maybe that was the issue then, but that's weird because even when I tried putting all the damage multiplier thingy without adding the condition that solved it into the zombie resource, it just wasn't working either somehow. Link to comment
Saml1er Posted June 14, 2018 Share Posted June 14, 2018 The code SP3CTR3 gave you is correct, you'll just need to change resourceRoot to root. Only buggy code will not work. Test it Link to comment
Galactix Posted June 14, 2018 Author Share Posted June 14, 2018 Not going to risk into that, I got the result I wanted by doing the way I did so I apreciate your support though SP3CTR3 and Saml1er! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now