Function doesn't check if player died

One of my scripts for a game is supposed to check if a player died and when a player dies to make a value set to true. It doesn’t work right and I was wondering how I can fix it.

function getPlayerStatus(Status)
	print("end me")
	local a = 0
	local people = game.Players:GetPlayers()
	for i = 1,#people do
		if people[i]:findFirstChild("Dead") then
			if people[i].Dead.Value == Status then
				a = a + 1
			end
		end
	end
	print(a)
	return a
end

Can’t you just use the Humanoid.Died function instead? You can implement a conditional check if the Dead.Value check is false, then set it to true afterwards

How many times are you calling the function? Currently it will only work for players who were in the game when the function was called.

How would I write that? I don’t know how.

Here’s a quick tip that will make your life easier.

Instead of: a = a + 1

Do: a += 1

image

Do something relevant to this:

local DeadPlayers = 0

game.Players.PlayerAdded:Connect(function(Player)
    local DeadCheck = false
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        Humanoid.Died:Connect(function()
            if DeadCheck == false then
                DeadPlayers += 1
                print(Player.Name.." has died!")
                DeadCheck = true
            else
                print(Player.Name.." has already died!")
            end
        end)
    end)
end)

You could pretty much implement what you wanna do with this when a Player’s Character dies

Like mentioned above you can use PlayerAdded then CharacterAdded then Character.Humanoid.Died:Connect()

Yes Humanoid.Died is the best way to check if someone die.