Hi, a noob scripter here! I’m making a round-based fighting game. I’ve finished coding the intermission time and fighting time. After the timer ends, players will be teleported back to the lobby. (This is what I have finished)
However, I found out that it would be quite annoying when the other players have died, and the last one standing would have to WAIT until the timer ends so that they can finally teleport back to lobby.
So the question is:
How can I make it so whenever there is 1 player ONLY on a specific map (I made 3 simple maps with a spawnpart), the timer can end/ set to 0, then players can teleport back to lobby (the original spawnpoint)?
you can loop trough all the players, get their humanoids and add them to a table
when a humanoid dies you can remove them from the table
if the table only has 1 entry you can end the round
here is a quick demonstration i made
local AlivePlayers = {}
for _,Player in game.Players:GetPlayers() do
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass('Humanoid')
table.insert(AlivePlayers,Player)
Humanoid.Died:Once(function()
local find = table.find(AlivePlayers,Player)
if find then
table.remove(AlivePlayers,find)
end
end)
if #AlivePlayers == 1 then
--uh end the round
end
end
here is the better code so you dont check in a loop(if u dont check in a loop its better for optimization, dont use lotta loops!)
local AlivePlayers = {}
for _,Player in game.Players:GetPlayers() do
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass('Humanoid')
table.insert(AlivePlayers,Player)
Humanoid.Died:Once(function()
local find = table.find(AlivePlayers,Player)
if find then
table.remove(AlivePlayers,find)
end
if #AlivePlayers == 1 then
--uh end the round
end
end)
end
you need to break the round loop, Right now the timer loop keeps running!
setting the ROUND_TIME to 0 is gonna set the duration for future rounds to 0, you can make a variable like END_ROUND and if the loop detects the variable as true it sets it to false and breaks the loop
you should put this in the RunGame function but does not really matter
If you have any more questions you can feel free to reach out!