Is there a way to respawn everyone after clear all children?

Im working on a game where when you touch a part everyone dies but after a certain amount of time they respawn, Is there a way to respawn them after I use game.players:ClearAllChildren()

Here is my full script

script.Parent.Touched:Connect(function()
	game.Players:ClearAllChildren()
end)
1 Like

simply reload all their characters by doing something like this:

for i,v in pairs(game.Players:GetChildren()) do
     v:LoadCharacter() --Code in this for loop will run for every player
end
1 Like

how would I put that in my script?

script.Parent.Touched:Connect(function(p)
	if (game.Players:GetPlayerFromCharacter(p.Parent)) then
		for _, player in ipairs(game.Players:GetPlayer()) do
			player:LoadCharacter()
		end
	end
end)

Oh I just realised you did clear all children, that pretty much breaks the game as the players wont be able to respawn, replace the clear all children with the reload character

How would I reload all characters of 2 teams?

you would add a check in the for loop of what their team is

(just in case youre not sure “v” is a variable for the player)

for i,v in pairs(game.Players:GetChildren()) do
     if v.Team == game:GetService("Teams").Team1 or v.Team == game:GetService("Teams").Team2 then
          v:LoadCharacter() --Code in this for loop will run for every player
     end
end

Would I change Team1 And Team2 to the team names?

Yes, because Team instances are named

1 Like

I changed the Team1 and Team2 to the names of the teams but it doesn’t seem to be working.

if v.Team == game:GetService("Teams").Sans or v.Team == game:GetService("Teams").Chara then

You are trying to delete every player which will just end up them disconnecting from the game. If you want to respawn every player use a generic for loop and run it on every player, and then use the method :LoadCharacter().

Try this

for _, player in ipairs(game.Players:GetPlayers()) do
    if player.Team == game.Teams["Sans"] or v.Team == game.Teams["Chara"] then
        player:LoadCharacter();
    end
end

You should also be using the method :GetPlayers() on the Players service instead of :GetChildren.

it doesn’t seem to be working

specificly this part

or v.Team == game.Teams["Chara"] then

Are Sans and Chara the only teams? If so, you won’t have to call an if statement and just load each of the player’s characters since judging from how you did it you’re trying to call :LoadCharacter on every player in that team.

I have one more team team called “Lobby”

I guess I could just respawn every player how would I do that?

for _, player in ipairs(game.Players:GetPlayers()) do
    if player.Team == game.Teams["Sans"] then
        player:LoadCharacter();
    elseif player.Team == game.Teams["Chara"] then
       player:LoadCharacter();
    end
end

Try this one out, and see if it makes any difference. If it still doesn’t work, open the output and tell me if there are any errors.

Thank you! It works but one last thing, if you could help me with, how would I change their teams to “Lobby” when they step on it

player.Team = game.Teams["Lobby"];