MTA.Castiel Posted April 23, 2023 Share Posted April 23, 2023 Hello community. I need to create some type of feature that rewards a player (attacker) for every kill. But before i could do so, I ran into a little issue, I need to figure out how to properly detect the attacker's source. Example: If i were to directly shoot and kill a player named Bobby with an Ak-47, script should return: Castiel killed Bobby (Ak-47) (Torso) (attacker element found) The Problem: If Bobby's sitting in a vehicle (driver seat or passanger seat) and i directly shoot the vehicle instead of Bobby (PED), the vehicle explodes, kills Bobbly. script returns: Bobby died. (attacker element not found) What would be the best way to detect the attacker in such an event, if even possible? function playerWasted_reward ( ammo, attacker, weapon, bodypart ) if ( attacker ) and ( attacker ~= source ) then local tempString if ( getElementType ( attacker ) == "player" ) then tempString = getPlayerName ( attacker ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" elseif ( getElementType ( attacker ) == "vehicle" ) then tempString = getPlayerName ( getVehicleController ( attacker ) ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" end if ( bodypart == 9 ) then -- give a special reward for headshots tempString = tempString.." (Headshot)" else -- give him / her a normal reward tempString = tempString.." ("..getBodyPartName ( bodypart )..")" end outputChatBox ( tempString ) -- if no attacker found then else else outputChatBox ( getPlayerName ( source ) .. " died." ) end end addEventHandler( "onPlayerWasted", getRootElement( ), playerWasted_reward ) Link to comment
FLUSHBICEPS Posted April 23, 2023 Share Posted April 23, 2023 u can use onVehicleDamage to store the last player who damaged the vehicle function vehicleDamage(attacker) if attacker and getElementType(attacker) == "player" then setElementData(source, "lastVehicleDamager", attacker) end end addEventHandler("onVehicleDamage", getRootElement(), vehicleDamage) when the vehicle gets exploded u can get the last player who damaged it function vehicleExplode() local attacker = getElementData(source, "lastVehicleDamager") setElementData(source, "lastVehicleAttacker", attacker) end addEventHandler("onVehicleExplode", getRootElement(), vehicleExplode) now to check if the attacker is a vehicle or unknown you need to modify playerwasted_reward func function playerWasted_reward(ammo, attacker, weapon, bodypart) if attacker and attacker ~= source then local tempString if getElementType(attacker) == "player" then tempString = getPlayerName(attacker) .. " killed " .. getPlayerName(source) .. " (" .. getWeaponNameFromID(weapon) .. ")" elseif getElementType(attacker) == "vehicle" then local vehicleAttacker = getElementData(attacker, "lastVehicleAttacker") if vehicleAttacker then tempString = getPlayerName(vehicleAttacker) .. " killed " .. getPlayerName(source) .. " (" .. getWeaponNameFromID(weapon) .. ")" else tempString = "A vehicle killed " .. getPlayerName(source) .. " (" .. getWeaponNameFromID(weapon) .. ")" end end if bodypart == 9 then -- give a special reward for headshots tempString = tempString .. " (Headshot)" else -- give him / her a normal reward tempString = tempString .. " (" .. getBodyPartName(bodypart) .. ")" end outputChatBox(tempString) else outputChatBox(getPlayerName(source) .. " died.") end end addEventHandler("onPlayerWasted", getRootElement(), playerWasted_reward) also recommending to reset the elementdatas when a vehicle is repaired or respawned to avoid old data from being used in future events 1 Link to comment
MTA.Castiel Posted April 23, 2023 Author Share Posted April 23, 2023 It sounds like it could actually work but I'm getting an error on the "vehicleDamage" function. When Bobby's vehicle is shot, it shows - Bad argument @ 'getElementType' [Expected element at argument 1, got number '30'] Link to comment
Tails Posted April 24, 2023 Share Posted April 24, 2023 (edited) 20 hours ago, MTA.Castiel said: - Bad argument @ 'getElementType' [Expected element at argument 1, got number '30'] Because @FLUSHBICEPS made a tiny mistake. The event doesn't return the player, it returns the damage the vehicle took. What you need to do instead, is use the getVehicleOccupant function to get the player. See https://wiki.multitheftauto.com/wiki/OnVehicleDamage for more info and an example. Edited April 24, 2023 by Tails 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