justn Posted March 15, 2014 Share Posted March 15, 2014 Hi, i would like it if theAttacker is in team medic, then the other player's health will go up. function healPlayerOnHit_Handler(theAttacker, theWeapon, theBodyPart, theLoss) if(getElementType(theAttacker) == "player") then if isPlayerInTeam(theAttacker,"Medic") then if(theWeapon == 41) then cancelEvent() local targetCurrentHealth = getElementHealth(source) local newHealth = targetCurrentHealth+25 -- Replace 5 with the amount the player should be healed. setElementHealth(source, newHealth) local randomCash = math.random ( 25, 50 ) givePlayerMoney(theAttacker,randomCash) end end end end addEventHandler("onPlayerDamage", getRootElement(), healPlayerOnHit_Handler) Link to comment
iPrestege Posted March 15, 2014 Share Posted March 15, 2014 Does the 'isPlayerInTeam' Function exists? Link to comment
justn Posted March 15, 2014 Author Share Posted March 15, 2014 So do it. Oops, sorry if i didnt mention the debugscript error. ERROR: Medic\server.lua:28: attempt to call global 'isPlayerInTeam' (a nil value) Link to comment
Bonsai Posted March 15, 2014 Share Posted March 15, 2014 That function is not existing as long as you didn't implement it. function isPlayerInTeam( thePlayer, teamName ) local pTeam = getPlayerTeam( thePlayer ) local fTeam = getTeamFromName( teamName ) if ( pTeam ) and ( fTeam ) then if ( pTeam == fTeam ) then return true end end end You could also use getPlayerTeam and compare it to medic team. Link to comment
justn Posted March 15, 2014 Author Share Posted March 15, 2014 Problem fixed, but i need help with one more thing, if the other's player health is 100, i want theAttacker to stop getting money Link to comment
Saml1er Posted March 15, 2014 Share Posted March 15, 2014 Please read wiki before you start using functions and events. Wiki says: It should be noted that this event is not triggered when attacked by a team member if friendly fire is enabled. It should also be noted that canceling this event has no effect. Cancel the client-side event onClientPlayerDamage instead. Link to comment
justn Posted March 15, 2014 Author Share Posted March 15, 2014 Please read wiki before you start using functions and events.Wiki says: It should be noted that this event is not triggered when attacked by a team member if friendly fire is enabled. It should also be noted that canceling this event has no effect. Cancel the client-side event onClientPlayerDamage instead. ._. Not everyone can be some 'experienced professional' at scripting, besides, I barely even script anyways, I usually play DM Racing and make maps Link to comment
Saml1er Posted March 15, 2014 Share Posted March 15, 2014 (edited) Please read wiki before you start using functions and events.Wiki says: It should be noted that this event is not triggered when attacked by a team member if friendly fire is enabled. It should also be noted that canceling this event has no effect. Cancel the client-side event onClientPlayerDamage instead. ._. Not everyone can be some 'experienced professional' at scripting, besides, I barely even script anyways, I usually play DM Racing and make maps I never said that every one can be professional or experienced. Wiki provides you the most info that's why I suggested you to read wiki. Nothing is hard here, just remove source since we're defining it in event handler and addEventHandler("onPlayerDamage", getRootElement(), healPlayerOnHit_Handler) to addEventHandler("onClientPlayerDamage", getLocalPlayer(), healPlayerOnHit_Handler ) and about the health you asked, just use bonsai's code and add a new line local targetCurrentHealth = getElementHealth() if not == 100 then this script is client side. Edited March 15, 2014 by Guest Link to comment
Moderators Citizen Posted March 15, 2014 Moderators Share Posted March 15, 2014 (edited) local targetCurrentHealth = getElementData () if not == 100 then You made a typo and better use >= just to be sure: local targetCurrentHealth = getElementHealth ( localPlayer ) if not targetCurrentHealth >= 100 then EDIT: Forwarding typo fix by Saml1er in case of a copy-paster comming through + another one Edited March 15, 2014 by Guest Link to comment
Saml1er Posted March 15, 2014 Share Posted March 15, 2014 local targetCurrentHealth = getElementData () if not == 100 then You made a typo and better use >= just to be sure: local targetCurrentHealth = getElementData () if not targetCurrentHealth >= 100 then I kinda thought of using it but since ped/player health is between 100 so I thought its not needed but anyway still using >= is useful because more checks less chances for script to fail. EDIT: The actual typo was "getElementData" because I wrote it instead of getElementHealth. Fixed it now. Link to comment
Moderators Citizen Posted March 15, 2014 Moderators Share Posted March 15, 2014 Hehe and another typo fix local targetCurrentHealth = getElementHealth ( localPlayer ) if not targetCurrentHealth >= 100 then Link to comment
justn Posted March 15, 2014 Author Share Posted March 15, 2014 The script does not work in clientside . So i tried it in serverside and it still does not work, Well, it works, but when I added the code if not targetCurrentHealth >= 100 then then it stopped working Link to comment
Saml1er Posted March 15, 2014 Share Posted March 15, 2014 The script does not work in clientside . So i tried it in serverside and it still does not work, Well, it works, but when I added the code if not targetCurrentHealth >= 100 then then it stopped working It doesn't works in clientside? What exactly doesn't work? and when you added the code then it worked but it stopped working? It worked and stopped working? How? Link to comment
justn Posted March 15, 2014 Author Share Posted March 15, 2014 The script does not work in clientside . So i tried it in serverside and it still does not work, Well, it works, but when I added the code if not targetCurrentHealth >= 100 then then it stopped working It doesn't works in clientside? What exactly doesn't work? and when you added the code then it worked but it stopped working? It worked and stopped working? How? the function healPlayerOnHit doesn't work in clientside, I have changed up the stuff like you told me and it doesn't work, and when i added this code to the function healPlayerOnHit, then the function stopped working properly if not targetCurrentHealth >= 100 then Link to comment
Saml1er Posted March 15, 2014 Share Posted March 15, 2014 The script does not work in clientside . So i tried it in serverside and it still does not work, Well, it works, but when I added the code if not targetCurrentHealth >= 100 then then it stopped working It doesn't works in clientside? What exactly doesn't work? and when you added the code then it worked but it stopped working? It worked and stopped working? How? the function healPlayerOnHit doesn't work in clientside, I have changed up the stuff like you told me and it doesn't work, and when i added this code to the function healPlayerOnHit, then the function stopped working properly if not targetCurrentHealth >= 100 then Post your script. Link to comment
justn Posted March 16, 2014 Author Share Posted March 16, 2014 Post your script function healPlayerOnHit_Handler(theAttacker, theWeapon, theBodyPart, theLoss) if(getElementType(theAttacker) == "player") then if isPlayerInTeam(theAttacker,"Medic") then if(theWeapon == 41) then cancelEvent() local targetCurrentHealth = getElementHealth ( source ) local newHealth = targetCurrentHealth+25 setElementHealth(source, newHealth) local randomCash = math.random ( 25, 50 ) givePlayerMoney(theAttacker,randomCash) end end end end end addEventHandler("onPlayerDamage", getRootElement(), healPlayerOnHit_Handler ) Link to comment
Saml1er Posted March 16, 2014 Share Posted March 16, 2014 Client side. function healPlayerOnHit_Handler (attacker, weapon, bodypart) if(getElementType(attacker) == "player") and isPlayerInTeam(attacker,"Medic") then if weapon == 41 then cancelEvent() if not targetCurrentHealth >= 100 then local targetCurrentHealth = getElementHealth ( ) local newHealth = 100 - targetCurrentHealth setElementHealth( newHealth) local randomCash = math.random ( 25, 50 ) givePlayerMoney(theAttacker,randomCash) end end end addEventHandler("onClientPlayerDamage", getLocalPlayer(), healPlayerOnHit_Handler ) Don't forget to modify your méta.xml. Link to comment
DNL291 Posted March 16, 2014 Share Posted March 16, 2014 @Saml1er you forgot to put attacker (theAttacker isn't defined) in givePlayerMoney function. Also, the player element is missing at getElementHealth and setElementHealth. And is missing an 'end' to close the function. And i don't think that will work, because of: local newHealth = 100 - targetCurrentHealth. Link to comment
justn Posted March 16, 2014 Author Share Posted March 16, 2014 E_e if anyone does not know how to fix this then nevermind i guess Link to comment
DNL291 Posted March 16, 2014 Share Posted March 16, 2014 Try this: Client addEventHandler("onClientPlayerDamage", localPlayer, function (attacker, weapon, bodypart, loss) local team = getPlayerTeam(attacker) if not team then return end if (attacker and getElementType(attacker) == "player" and weapon == 41 and getTeamName(team) == "Medic") then cancelEvent() if (not isTimer(pause)) then local health = getElementHealth(localPlayer) if (health < 99) then triggerServerEvent("medic:healing", localPlayer, attacker) pause = setTimer(function() end, 800, 1) end end end end) Server: addEventHandler("medic:healing", root, function (medic) if (getElementHealth(source) < 100) then local Heal = getElementHealth(source) + 11 -- New health setElementHealth(source, Heal) if (Heal > 100) then setElementHealth(source, 100) end local money = math.random(25, 50) givePlayerMoney(medic, money) takePlayerMoney(source, money) end end) Link to comment
justn Posted March 16, 2014 Author Share Posted March 16, 2014 Try this:Client addEventHandler("onClientPlayerDamage", localPlayer, function (attacker, weapon, bodypart, loss) local team = getPlayerTeam(attacker) if not team then return end if (attacker and getElementType(attacker) == "player" and weapon == 41 and getTeamName(team) == "Medic") then cancelEvent() if (not isTimer(pause)) then local health = getElementHealth(localPlayer) if (health < 99) then triggerServerEvent("medic:healing", localPlayer, attacker) pause = setTimer(function() end, 800, 1) end end end end) Server: addEventHandler("medic:healing", root, function (medic) if (getElementHealth(source) < 100) then local Heal = getElementHealth(source) + 11 -- New health setElementHealth(source, Heal) if (Heal > 100) then setElementHealth(source, 100) end local money = math.random(25, 50) givePlayerMoney(medic, money) takePlayerMoney(source, money) end end) Works, thanks ! I had to add the event "medic:healing" to the serverside since you forgot to add it. Link to comment
DNL291 Posted March 16, 2014 Share Posted March 16, 2014 You're welcome. I had to add the event "medic:healing" to the serverside since you forgot to add it. Yeah, my bad. Link to comment
Saml1er Posted March 16, 2014 Share Posted March 16, 2014 You're welcome.I had to add the event "medic:healing" to the serverside since you forgot to add it. Yeah, my bad. I have a question. When you define local Player as source in évent handler and when you use other functions inside it then Player élément has to defined again for other functions (like getElementHealth etc)? I'm asking this because I've never tested it in that way but I saw it on wiki several times. Link to comment
DNL291 Posted March 17, 2014 Share Posted March 17, 2014 I have a question. When you define local Player as source in évent handler and when you use other functions inside it then Player élément has to defined again for other functions (like getElementHealth etc)? I'm asking this because I've never tested it in that way but I saw it on wiki several times. Sorry, i didn't understand exactly what you mean. 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