I was making a sword for my game and basically everything works except for when you kill someone, your kills keep adding more than 1 kill to your leaderstats. For example, if I killed one player, it would set my kills to 5 instead of one. Anybody able to help?
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if properties["Can Damage"] == true then
properties["Can Damage"] = false
local attacker = character
local victim = hit.Parent
if attacker ~= victim then
if victim.Humanoid.Health >0 then
if marketPlaceService:UserOwnsGamePassAsync(player.UserId,22882690) then
victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage * 2
else
victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage
end
local obj = victim.Humanoid
obj.Died:Connect(function()
player.leaderstats.KOs.Value = player.leaderstats.KOs.Value + 1
player.leaderstats.Streak.Value = player.leaderstats.Streak.Value + 1
print(victim.Name.." has died")
game.ReplicatedStorage:FindFirstChild("ViewKiller"):FireClient(players:GetPlayerFromCharacter(victim),player)
end)
end
end
end
end
end)
Because .Touched will happen each time a Part touches the item. This can happen a dozen times almost instantly when a player touches a Part.
Just as @IcyMizu stated, use a debounce.
U should check the candamage value in the died event so every time the died event triggers it sees if the candamage value is true or not and if it is it can pass if it isn’t it wont pass
It should be reset to true at the end of the .Touched function with a short wait(x) just before it, otherwise the touch will keep repeating in that split second that it takes until it’s reset to true. That’s what a Debounce Patterns | Roblox Creator Documentation is supposed to counteract.
(edited)
well you know every time kill some one , you create another event obj.Died. This event will stay forever if you did not disconnect it, then it get double every time you kill some one.
local conn = nil
handle.Touched:Connect(function(hit)
if conn then conn:Disconnect() end
if hit.Parent:FindFirstChild("Humanoid") then
if properties["Can Damage"] == true then
properties["Can Damage"] = false
local attacker = character
local victim = hit.Parent
if attacker ~= victim then
if victim.Humanoid.Health >0 then
if marketPlaceService:UserOwnsGamePassAsync(player.UserId,22882690) then
victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage * 2
else
victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage
end
local obj = victim.Humanoid
conn = obj.Died:Connect(function()
player.leaderstats.KOs.Value = player.leaderstats.KOs.Value + 1
player.leaderstats.Streak.Value = player.leaderstats.Streak.Value + 1
print(victim.Name.." has died")
game.ReplicatedStorage:FindFirstChild("ViewKiller"):FireClient(players:GetPlayerFromCharacter(victim),player)
end)
end
end
wait(.1)
properties["Can Damage"] == true
end
end
end)