Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Checking if all players died?

Asked by
Hero_ic 502 Moderation Voter
9 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 9 years ago

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

01game.Players.PlayerAdded:connect(function(player)
02    t=Instance.new("BoolValue", player)
03    t.Name="Died"
04    player.CharacterAdded:connect(function()
05        player.Character.Humanoid.Died:connect(function()
06            player.Died.Value=true
07            trol=0
08            for i,v in pairs (game.Players:GetChildren()) do
09                if v.Died==true then
10                    trol=trol+1
11                end
12            end
13            num=game.Players:GetChildren()
14            if trol=<#num then
15                --do stuff...
View all 22 lines...

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

01ppl={}
02game.Players.PlayerAdded:connect(function(player)
03    player.CharacterAdded:connect(function()
04        player.Character.Humanoid.Died:connect(function()
05            if not ppl[player.Name] then
06                ppl[player.Name]=true
07            end
08            num=game.Players:GetChildren()
09            if  #ppl>=#num then
10                --do stuff
11                ppl={}
12            end
13        end)
14    end)
15end)

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 — 9y
0
No prob, any time! BobserLuck 367 — 9y
Ad
Log in to vote
3
Answered by 9 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.

01local AlivePlrs = {}
02game.Workspace.ChildAdded:connect(function(Char)
03    if Char:IsA("Model") and Char:FindFirstChild("Humanoid") then -- Make sure it's a Character, not something else
04        local Humanoid = Char.Humanoid
05        table.insert(AlivePlrs, #AlivePlrs + 1, Char.Name)
06        Humanoid.Died:connect(function()
07            for Position, Plr in ipairs(AlivePlrs) do
08                if Plr == Char.Name then -- If they are in the table then...
09                    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
10                end
11            end
12        end)
13    end
14end)

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.

0
What is #AlivePlrs and why is a hashtag there? Also, is a table in Lua the same as a list in Python? wolffdonnaven 12 — 4y

Answer this question