when I kill the character with a sword, I want the number of kills to increase by 1
after the player dies, if you keep hitting, he gives more kills how can I fix this problem?
record:
script:
script.Parent.Handle.Touched:Connect(function(touch)
if script.Parent.CanDamage.Value == true then
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if not touch.Parent:FindFirstChild("Humanoid") then
humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid.Name ~= "Humanoid" then return end
script.Parent.CanDamage.Value = false
humanoid:TakeDamage(20)
if humanoid.Health < 1 then
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end
wait(1)
script.Parent.CanDamage.Value = true
end
end)
you could try to change your: humanoid.Health < 1
to: humanoid.Health > 0
this makes it so that it only adds when their health is above 0 rather than adding if their health is below 1. checking if their health is below one results in the script adding 1 to the leader stats since the humanoid you hit has a health below 1.
you can put a check before adding 1 kill to ensure they are not already dead. It would be something like this:
if humanoid.Health < 1 then
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
if plr.Character.Humanoid.Health > 0 then -- new check. if their health is above 0 then give opponent a kill.
plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end
end
You would need to set up .Died connections of players characters
so where a player .Died, you can award the killer with a kill, you would also have to make a tag system so that on hit, the object value of the hitter (killer) is stored.
of course, you could just check if the incoming damage would kill the target, then add a kill and kill the target then
i.e.
if Humanoid.Health<=0 then return end
--if incoming damage is lethal
if Humanoid.Health-20 <=0 then
--add to killstat
end
Humanoid:TakeDamage(20)
script.Parent.Handle.Touched:Connect(function(touch)
if script.Parent.CanDamage.Value == true then
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if not touch.Parent:FindFirstChild("Humanoid") then
humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid.Name ~= "Humanoid" then return end
script.Parent.CanDamage.Value = false
if humanoid.Health <= 0 then return end
if humanoid.Health-20 <= 0 then
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end
humanoid:TakeDamage(20)
wait(1)
script.Parent.CanDamage.Value = true
end
end)
if humanoid.Health < 1 and humanoid:FindFirstChild("Killed") == nil then
local killedTag = Instance.new("BoolValue")
killedTag.Name = "Killed"
killedTag.Parent = humanoid
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end
Dead = false
script.Parent.Handle.Touched:Connect(function(touch)
if script.Parent.CanDamage.Value == true and not Dead then
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if not touch.Parent:FindFirstChild("Humanoid") then
humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid.Name ~= "Humanoid" then return end
script.Parent.CanDamage.Value = false
humanoid:TakeDamage(20)
if humanoid.Health < 1 then
Dead = true
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
end
wait(1)
script.Parent.CanDamage.Value = true
end
end)
script.Parent.Handle.Touched:Connect(function(touch)
if script.Parent.CanDamage.Value then
local humanoid = touch.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = touch.Parent.Parent:FindFirstChild("Humanoid")
end
if not humanoid or humanoid.Name ~= "Humanoid" then
return
end
script.Parent.CanDamage.Value = false
humanoid:TakeDamage(20)
if humanoid.Health <= 0 then
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
if plr then
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local kills = leaderstats:FindFirstChild("Kills")
if kills and kills:IsA("IntValue") then
kills.Value = kills.Value + 1
end
end
end
end
wait(1)
script.Parent.CanDamage.Value = true
end
end)