How to teleport all players back to lobby when there is a winner in the round-based fighting game?

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)?

Here are my scripts:

Any suggestions are appreciated!!! :smiley:

3 Likes

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

hope this helps

2 Likes

ohh sorry wait i accidently did somthing wrong :sob:

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

its still not the best but its better!

1 Like

Like this? It’s not working though.. What did i do wrong?

I tried resetting my character but the game time still runs

1 Like
  1. 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

  2. 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!

1 Like

Im sorry but I have literally no idea how to break the loop and set timer to 0 or skip the timer :sob: helppp

1 Like

you can break a loop by using break
you can go like

if END_ROUND then
   END_ROUND = false
   break
end

inside the loop(put this code inside the loop u set the clock attribute
and instead of

ROUND_TIME = 0

when you wanna end the round

do

END_ROUND = true

hope this helps

That’s a really bad example because that isn’t a loop.

2 Likes

yeah im a bit bad at explaining things :sob:
what i mean is put that code in
image
this part of the code inside the loop
Ty for the headsup tho : D

1 Like

It worked perfectly!! TYSM but I don’t quite understand why this works. There’s something I’d like to ask!

  1. Why should the for loop (the one you made) be put inside the countdown() function? Like why does it work?

  2. Why “break” works in this script? Does it just break (stops) the function countdown()? Because I’m not familiar with “break”.

  3. Why does this only work inside the game round? (which is exactly what i want dw) Like when in INTERMISSION time, any death doesn’t reset the timer.

Sry if this is too much but I am eager to learn! Any explanation appreciated

Okay soo sorry if you dont understand a bit but uhh

  1. the break line breaks the loop and continues the code

FOR EXAMPLE:
If you run

for i=1,10,1 do
	print(i)
	task.wait()
	if i == 5 then
		break
	end
end
print('wow')

instead of it going

1
2
3
4
5
6
7
8
9
10
wow

it will go

1
2
3
4
5
wow

in the command bar cause the loop breaks at 5
to learn more you can look into
https://create.roblox.com/docs/luau/control-structures#break-loops

  1. no as i told in 1 it breaks the loop not the function

  2. uhh i dont really know what you mean but probably the function running before the round?? :sob:

Ohhh i get it now.. Thank you so much!!! :smiley:

1 Like

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