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)
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]
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
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)
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.
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.
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)