roaddog Posted February 25, 2015 Share Posted February 25, 2015 Due to this https://wiki.multitheftauto.com/wiki/On ... WeaponFire event does not work on melee weapon. How can I make a simple function that does the trick for it? I want to detect a player who hits an element using shovel weapon. Thanks. Link to comment
JR10 Posted February 25, 2015 Share Posted February 25, 2015 Use onClientPlayerDamage. Link to comment
xeon17 Posted February 25, 2015 Share Posted February 25, 2015 Maybe onClientPlayerDamage? EDIT: JR10 was faster Link to comment
roaddog Posted February 25, 2015 Author Share Posted February 25, 2015 No, I mean, The source of this event is the element that got damaged. Link to comment
JR10 Posted February 25, 2015 Share Posted February 25, 2015 onClientPlayerDamage provides the attacker and his weapon as an argument. Link to comment
xeon17 Posted February 25, 2015 Share Posted February 25, 2015 And what's the problem? Link to comment
roaddog Posted February 25, 2015 Author Share Posted February 25, 2015 the element is an object. I found this https://wiki.multitheftauto.com/wiki/On ... jectDamage, and I got an error says source is nil? eventho the element was created. function outputLoss(loss) if not isElement(source) then return end if getElementModel(source) ~= 897 then return end local oldHealth = getElementHealth(source) local hitX, hitY, hitZ = getElementPosition(source) setTimer(function() local newHealth = getElementHealth(source) outputChatBox("Real loss: "..(newHealth-oldHealth)) outputChatBox("Theoretical loss: "..loss) if getElementHealth(source) == 0 then destroyElement(source) createExplosion(hitX, hitY, hitZ, 8, true, -1.0, false) end end,100,1) end addEventHandler("onClientObjectDamage", root, outputLoss) Link to comment
JR10 Posted February 25, 2015 Share Posted February 25, 2015 You're using source inside the timer function, source is only defined inside the handler function. Just define a player variable that points to the source. Or you can send the source as an argument to the timer function. function outputLoss(loss) if not isElement(source) then return end if getElementModel(source) ~= 897 then return end local oldHealth = getElementHealth(source) local hitX, hitY, hitZ = getElementPosition(source) local object = source setTimer(function() local newHealth = getElementHealth(object) outputChatBox("Real loss: "..(newHealth-oldHealth)) outputChatBox("Theoretical loss: "..loss) if getElementHealth(object) == 0 then destroyElement(object) createExplosion(hitX, hitY, hitZ, 8, true, -1.0, false) end end,100,1) end addEventHandler("onClientObjectDamage", root, outputLoss) Link to comment
roaddog Posted February 25, 2015 Author Share Posted February 25, 2015 Thanks, It works o/ 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