How would I make the last player alive win?

So I want to make a simple game (for Example a Battle Royal) where the last player alive wins. So far ive looked every where and nothing. Would I use teams and some how get the players on that team? Please help me. Thanks

5 Likes

One thing that you could do is to have a table with the participants. Each time someone dies, remove them from the table.

3 Likes

Have you atleast made a start? There is loads of tutorials on youtube! Dont just come to the DevForum asking people to make stuff for you.

1 Like

Yes they dont work Ive looked on youtube google and on here my way dosesent work while changing it

plus i never said write for me i said i need help

You can get the number of players in a team using Team:GetPlayers()

after that all you’ve got to do is iterate, preferably in ipairs, through every player and see what the health of each player’s humanoid is, if it = 0 then it means the player’s dead, else the player’s alive.

pseudo code :

local teams = game:GetService("Teams"):GetTeams()
      for _, team in ipairs(teams) do
             local players = team:GetPlayers()
 --// check whether the player is alive for a team
1 Like

It says NIL to find Humanoid what am I doing wrong image

You could use the WaitForChild function to wait until the humanoid exists. One thing I suggest is to use the Humanoid.Died event to detect when the Humanoid dies.

1 Like

Ive tryed useing that event once but for some reason when I use it it dosesnt work

1 Like

local players = team:GetPlayers()

returns a table of players
You would have to loop through that again to listen in for their died event and then you could set their team and disconnect the event

for _,plr in ipairs(players) do
    local char = plr.Character or plr.CharacterAdded:Wait() -- Waiting for players character.
    local humanoid = char:FindFirstChildOfClass("Humanoid") -- just incase it's named something else
    
   -- They are valid.
    if humanoid then
        local x;
        x = humanoid.Died:Connect(function()
              -- The player just died.
              plr.Team = game.Teams.Lobby -- setting their team.
              x:Disconnect() -- cleaning up the event.
        end)
    end
end

I have not actually tested this script, but it should work for the most part if anyone else has anything to add that’d be great

1 Like

local players = team:GetPlayers() where are you getting team from just so I learn?

From the loop you wrote above.

1 Like

image
the local players is Nil says the output then it ends the script

1 Like

as I said before you still need to keep your loop for the teams
and then you can nest that loop within it

for _, team in ipairs(teams) do
      local players = team:GetPlayers()
      -- put the player loop in here
end

It still says Local players is a NIL value

Yes just what my script showed earlier, but that’s not the problem.

@YumReesesPuffs
Try using :WaitForChild() to prevent an instance from being indexed before instantiating, as that could throw errors.

Try to check for whether the player or character even exists before trying to index Humanoid or you’ll be indexing with nil.

2 Likes

You are looping the teams, not the players. When team:GetPlayers() returns, you have to loop through that table.

for _,team in pairs(teams) do
   local Players = team:GetPlayers()
   for _,plr in pairs(Players) do
      local c = plr.Character or plr.CharacterAdded:Wait()
      local Humanoid = c:WaitForChild("Humanoid")
      local x
      x = Humanoid.Died:Connect(function()
         plr.Team = game.Teams.Lobby
         x:Disconnect()
      end)
   end
end
1 Like