Basically, the table im using is named Winners. This table represents the last player standing in the battlefield(players in the Duelers team). I tried to use the table.insert function to add the last player to the table when there is only one player left, but doesn't seem to work? Does anyone know exactly what i did wrong? Im very new to the use of tables, so yeah....The script is here below:
PS. the status.value thing is a textlabel that provides the game status. Y'know, its in practically in every game. It shows the intermission time and stuff like that..
local PlayersInMatch = game.Teams.Duelers:GetPlayers() local Winners = {} if PlayersInMatch == 1 then table.insert(Winners, 1, PlayersInMatch) end for i = 200,1,-1 do if #PlayersInMatch == 1 then break end status.Value = "Time Left: "..i wait(1) end status.Value = "Game End! The Winner is "..table.concat(Winners, ' ')
table.insert
isn't your problem; you're using it correctly. However, there are other problems with this, and you may not even need to use it:
The code which checks for the PlayersInMatch
runs once, when the script is first ran. The amount of players on the team at that time is most likely zero; server scripts run before local scripts do because the server loads before the player.
When you call GetPlayers()
, it returns a table of players in that team. This table does not change when the amount of players in that team changes, PlayersInMatch
is still the amount of players on the team when you called it.
You can remedy this by moving that code to the appropriate location, so that it gets the amount of players within the loop, instead of only once, when the script runs.
There's also an issue with the way you use table.concat
; if the code were to behave as you thought, table.concat
would have tried to concatenate a userdata value (the Player), which would have caused an error. Really, you don't even need that here at all, what you may have been looking for is tostring
1.
Lastly, a small logic error, your code assumed that there's at least one player left, but there could be no players left. You can easily handle this, as well.
local Winner; for i = 200,1,-1 do local PlayersInMatch = game.Teams.Duelers:GetPlayers() if #PlayersInMatch <= 1 then Winner = PlayersInMatch[1]; break end status.Value = "Time Left: "..i wait(1) end status.Value = (Winner) and ("Game End! The Winner is "..tostring(Winner)) or ("No one survived!") --// Ternary operator.
Notice how your code looks a bit cleaner? I fixed it for you this time, but please be sure to properly indent your code in the future.
Hope this helped.
PS: You can read more about the ternary operator I used above, if you'd like.