You know how annoying it is when another player steals your kill and you get nothing? So, I want to have an kill assist to make both players (one who dealt the most damage and who hit the last) get lowered rewards or something.
What I want to know is how you implement this in code? Like I know how you see who killed the player(creator), but I have no idea how you can check who dealt any damage previously.
I will appreciate any help provided! Also if I didn’t clarify some information, let me know!
Keep a list of recent hits for each player. When a player is hit, put the player who hit him in the list. When player dies, most recent hit takes the kill and if there is a second or third player who hit him too within a certain timeframe, give them the assist.
After a weapon applies damage to a humanoid you should then tag that humanoid with a creator tag referencing the player who did the damage, as well as another value that references how much damage was done.
I wrote a function that does this for you:
function tagHumanoid(humanoidThatTookDamage, playerWhoDidDamage, damageDone)
if humanoidThatTookDamage ~= nil then
local Tags=humanoidThatTookDamage:GetChildren()
for i=1,#Tags do
local Tag=Tags[i]
if Tag:IsA("ObjectValue") then
if Tag.Value==playerWhoDidDamage then --if creatortag already exists and is equal to playerWhoDidDamage then just add the damage to the existing damage value
Tag.damage.Value+=damageDone
return --exit function so it doesnt add a new tag
end
end
end
local creatorTag=Instance.new("ObjectValue")
creatorTag.Name="creator"
creatorTag.Parent=humanoidThatTookDamage
creatorTag.Value=playerWhoDidDamage
local damageTag=Instance.new("NumberValue")
damageTag.Name="damage"
damageTag.Parent=creatorTag
creatorTag.Value=damageDone
task.wait(5)--removes tag after set amount of time (bad practice)
--a better practice would be to remove all creatortags when a player is fully healed
--you can add that inside of a custom health script
creatorTag:Destroy()
end
end
All you need to do now is write some code that checks for any creator tags when players die.
If more than one creator tag exists you then should compare the damage values inside of each one and then award players accordingly.
Sorry for a random reply, I’ve finally started working on it. One more problem showed up: if I add this code to every weapon in my game, it would probably be not optimized well.
What I think might be the solution is to place this code into that script below, but can you give me some advice how to insert it properly, please?
Code:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local Humanoid = player.Character:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
local DamageDealer = Humanoid:FindFirstChild("creator")
if DamageDealer and DamageDealer.Value then
local Killer = game.Players:FindFirstChild(DamageDealer.Value.Name)
if Killer and Killer.leaderstats then
if Killer == player.Name then end
local kills = Killer.leaderstats:FindFirstChild("Kills")
if kills then kills.Value = (kills.Value or 0) + 1 end
local previous1 = Killer:GetAttribute("KillsThatRound")
Killer:SetAttribute("KillsThatRound", previous1 + 1)
end
end
local previous2 = player:GetAttribute("DeathsThatRound")
player:SetAttribute("DeathsThatRound", previous2 + 1)
end)
end)
end)
So, I found the solution! Still that function needs to be where :TakeDamage() thing exists, and I corrected code a bit to exclude some unwanted errors I stumbled upon when testing it.
Big thanks to you, @cleventa , for helping me finding the solution! It would’ve been awesome to make an awarding system like in steam reviews where you can grant people steam points!
Here’s the script that seems to be working fine in my complicated scenario:
local function tagHumanoid(humanoidThatTookDamage, playerWhoDidDamage, damageDone)
if humanoidThatTookDamage ~= nil then
local creatorTag=Instance.new("ObjectValue")
creatorTag.Name = "creator"
creatorTag.Parent = humanoidThatTookDamage
creatorTag.Value = playerWhoDidDamage
local damageTag = Instance.new("NumberValue")
damageTag.Name = "damage"
damageTag.Paren t= creatorTag
damageTag.Value = damageDone
local Tags=humanoidThatTookDamage:GetTags()
for i=1,#Tags do
local Tag=Tags[i]
if Tag then
if Tag.Value==playerWhoDidDamage then
Tag.damage.Value += damageDone
return
end
end
end
--print(creatorTag.Value, damageTag.Value)
humanoidThatTookDamage.Died:Connect(function()
--make there a remote event to connect with the server to handle all calculations
--warn("I am dead", humanoidThatTookDamage, playerWhoDidDamage, "he did", damageDone, "damage!")
creatorTag:Destroy()
end)
end
end