New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Checking if all players died?

Asked by
Hero_ic 502 Moderation Voter
3 years ago

How would I be able to check if all players died? I was thinking of adding a boolvalue into each player and once they die it'd set the value to false. Is there any better ways of doing it then this? Thanks. ~KIheros

2 answers

Log in to vote
0
Answered by 3 years ago

Well, using your example it's actually easier than you think:

game.Players.PlayerAdded:connect(function(player)
    t=Instance.new("BoolValue", player)
    t.Name="Died"
    player.CharacterAdded:connect(function()
        player.Character.Humanoid.Died:connect(function()
            player.Died.Value=true
            trol=0
            for i,v in pairs (game.Players:GetChildren()) do
                if v.Died==true then
                    trol=trol+1
                end
            end
            num=game.Players:GetChildren()
            if trol=<#num then
                --do stuff...
                for i,v in pairs(game.Players:GetChildren())do
                    v.Died.Value=false
                end
            end
        end)
    end)
end)

There's a direct bool example, or you can use a good o'll table to do the same type of thing:

ppl={}
game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function()
        player.Character.Humanoid.Died:connect(function()
            if not ppl[player.Name] then
                ppl[player.Name]=true
            end
            num=game.Players:GetChildren()
            if  #ppl>=#num then
                --do stuff
                ppl={}
            end
        end)
    end)
end)

Try it out but I won't guarantee it will work XD

0
Thank you, you gave me a good idea on how to do it! :) Hero_ic 502 — 3y
0
No prob, any time! BobserLuck 367 — 3y
Ad
Log in to vote
3
Answered by 3 years ago

There are multiple way you may see which players are alive, like if you made a game and wanted to see if a player died in that round or not, if so, reward, if not, don't reward. A good way is to make a table at the start and on the death of the player, remove their name.

local AlivePlrs = {}
game.Workspace.ChildAdded:connect(function(Char)
    if Char:IsA("Model") and Char:FindFirstChild("Humanoid") then -- Make sure it's a Character, not something else
        local Humanoid = Char.Humanoid
        table.insert(AlivePlrs, #AlivePlrs + 1, Char.Name)
        Humanoid.Died:connect(function() 
            for Position, Plr in ipairs(AlivePlrs) do
                if Plr == Char.Name then -- If they are in the table then...
                    table.remove(AlivePlrs, Position) -- Position is an iterator which gives a number, in this case we can use that number to find where their name is in this table and remove it
                end
            end
        end)
    end
end)

This is a Script btw. The Script I gave above is the basic Idea, of course this would allow a new player who joined the game during a round to be assigned to being Alive, giving them a reward when you probably wouldn't want them to count. I just gave the basic idea, not the whole framework of a game and it's Logic.

Answer this question