local deathcount = 0
local debounce = false
game:GetService("Players").PlayerAdded:Connect(function(Player)
game:GetService("ServerStorage").HitPerson.Event:Connect(function(Character)
if Player then
if Player.Team == game:GetService("Teams")["A"] then
Character.Humanoid.Died:Connect(function()
if debounce == false then
debounce = true
deathcount = deathcount + 1
print(deathcount)
debounce = false
end
end)
end
end
end)
end)
Its supposed to only trigger once, but it triggers more than once, and it can still trigger when someone hits the body too.
As far as I can tell, you’re connecting the event every time a player joins, so it will trigger once for every player that has joined.
Perhaps, get the Player using “GetPlayerFromCharacter” which, as it suggests, gets the player from the character. This doesn’t return any errors if the character is not valid, it’ll just return nil, so we can use it as such:
local Players = game:GetService("Players")
game:GetService("ServerStorage").HitPerson.Event:Connect(function(Character)
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
if Player.Team == game:GetService("Teams")["A"] then
Character.Humanoid.Died:Connect(function()
if debounce == false then
debounce = true
deathcount = deathcount + 1
print(deathcount)
debounce = false
end
end)
end
end
end)
I don’t think there’s particularly something wrong with your code, it’s just that there’s no logical use for to debounce here - try and implementing it, so the debounce is set to false after the character respawned.
Nevermind. My bad. You’re listening for the character’s death every time that event is fired.
You shouldn’t need to do that every time
You shouldn’t even need a debounce, you should connect a new function every time the character is added, so I’ve used “Once” which disconnects the function when it’s triggered. This should do what you want
local deathcount = 0
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Once(function()
if Player.Team == game:GetService("Teams")["A"] then
deathcount = deathcount + 1
end
end)
end)
end)
Also, we’re checking the team when they die rather than when they spawn. Hope this helps
The event fires only when the player’s Health is below 0. Every hit damages the player, therefore going under 0. But I think you’re right with the solution you posted a few minutes ago.
Yes that is correct, but since they’ve connected it to multiple functions, a single death will make it fire all of those functions (which are all identical, and add 1 to the players death)