How would I get someone’s placement in a match when they die?

So I’m trying to make a script where when someone dies it gets their place in the match. when a game starts everyone is put in a team called: Playing and when they die they get sent to a team called Lobby

But I’m having trouble figuring out what to do next here.

This is my code currently:

remEvents.DeathEvent.OnServerEvent:Connect(function(plr)
    print("I DIED SERVER - "..plr.Name)
    if plr.Team == teams.Playing then
        plr.Team = teams.Lobby
        local getTeamLength = teams.Playing:GetPlayers()
        for i, v in pairs(getTeamLength) do
            print(i.." - "..v.Name)
        end

        if plr.userVals.placement.Value == 2 then
            remEvents.endGame:FireClient(plr)
        end
    end
end)
4 Likes

What does this mean?

like i want my script to know if someone got 1st or 2nd or 3rd

They’re trying to understand how to make the game recognize in which order a player was eliminated.

Store it in a table

local winners = {}

DeathEvent.OnServerEvent:Connect(function(player)
   table.insert(winners,1,player)
end)

table.insert(winners,1,player) will store the last player that died in the first spot in the table. So once all players have died the table will be sorted in order from last that died to first that died.

local firstPlace = winners[1]
local secondPlace = winners[2]
local thirdPlace = winners[3]

okay! so I’ve added that + this snippet to set the placement. But im getting this warning

Code:

local getPlrInTable = table.find(winners, plr)
plr.userVals.placement.Value = winners[getPlrInTable]

Error:
value of type Instance cannot be converted to a number

I tried fixing this but I’m stumped. Any ideas?

1 Like

Value is a number and needs to be set as such. So you could for loop through the winners table, check if the player’s name matches the value, if it does then the index is equal to the player’s position. Now you have a number and can set the placement Value.

for i,v in pairs(winners) do 
   if v == plr then 
      plr.userVals.placment.Value = i
      break
   end
end
1 Like

Ok.

Still stumped but I got over the value thing.

However it’s still not working the way I want as when someone dies it stores their placement as 0.

This is the best that I have:

local winners = {}


remEvents.DeathEvent.OnServerEvent:Connect(function(plr)
    print("I DIED SERVER - "..plr.Name)
    if plr.Team == teams.Playing then
        plr.Team = teams.Lobby
        
        for i, v in pairs(teams.Playing:GetPlayers()) do
            table.insert(winners, i, v)
        end
        print(winners)

        if #teams.Playing:GetPlayers() == 1 then
           --remEvents.endGame:FireClient(plr, winners) -- idk if i should use FireClient or FireAllClients
            remEvents.endGame:FireAllClients(winners)
        end
    end
end)
1 Like

Could you loop through the winners table and print i,v for me?

for i,v in pairs(winners) do 
     print(i,v)
end

Edit: Why are you looping through all the players on a team and putting them into the winners table when only one player dies?
Edit 2: In your script when someone dies you send them to the lobby, and then loop through the remaining players on a team and add them to the winners table. That would create duplicates of players inside of the winners table.

1 Like

So the print print returns as:
1 Player1

And I’m unsure on how else to calculate when theres 1 person left in the game.

I could do Teams.Playing.Changed to tell when theres 1 person left. but then how would I get the users place when they die

local winners = {}


remEvents.DeathEvent.OnServerEvent:Connect(function(plr)
    print("I DIED SERVER - "..plr.Name)
    if plr.Team == teams.Playing then
         for i, v in pairs(teams.Playing:GetPlayers()) do
            if v == plr then 
                  table.insert(winners, v)
                  break
              end
          end
       plr.Team = teams.Lobby

        if #teams.Playing:GetPlayers() == 1 then
           --remEvents.endGame:FireClient(plr, winners) -- idk if i should use FireClient or FireAllClients
            remEvents.endGame:FireAllClients(winners)
        end
    end
end)

table.insert used without the position will put it either at the top or bottom of the array (can’t remember) and then when you find who won just loop through the table and either get the top three or bottom three names. Also when a player joins are leaves the game check the length of the playing team.

Edit: I have to do something so sorry for the rushed response and late replies.

1 Like

so I finally got it working with the help of you and batteryday

This is my deathhandler code:

local RepStorage = game:GetService("ReplicatedStorage")
local remEvents = RepStorage.RemoteEvents
local teams = game:GetService("Teams")
local winners = {}


remEvents.DeathEvent.OnServerEvent:Connect(function(plr)
    print("I DIED SERVER - "..plr.Name)
    if plr.Team == teams.Playing then
        for i, v in pairs(teams.Playing:GetPlayers()) do
            if v == plr then 
                table.insert(winners, v)
                break
            end
        end
        
        plr.Team = teams.Lobby

        if #teams.Playing:GetPlayers() == 1 then
            table.insert(winners, teams.Playing:GetPlayers()[1])
            remEvents.endGame:FireAllClients(winners)
            print(winners)
        end
    end
end)

And my gameHandler code:

repStorage.RemoteEvents.endGame.OnServerEvent:Connect(function(plr, winners)
   print("Game Ended")

   for i = 1, math.floor(#winners/2) do
      local j = #winners - i + 1
       winners[i], winners[j] = winners[j], winners[i]
   end

   for i, v in pairs(winners) do
      local getPlrInTable = table.find(winners, v)
      v.userVals.placement.Value = getPlrInTable
      v.Team = teams.Lobby
      v.Character:MoveTo(Vector3.new(-180.88, 3.421, -222.996))
   end

   if winners[1] == plr then
      plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 750
      plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1

   elseif winners[2] == plr then
      plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 500

   elseif winners[3] == plr then
      plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 250

   end
end)

Genuinely thank you guys so much!

3 Likes

Happy to help! Make sure you clear the winners table when you’re done though. : )

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.