Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I end a match of something after all players but one are dead?

Please help.

The way I want it to be like (does not have to be like this!) Once players are teleported somewhere then they die, and there is one player left touching some bricks then the script should teleport the player back.

1 answer

Quite easily, really.

One way you could accomplish this is by adding all players to a table upon the match beginning, as well as connecting a Died event with each of their Humanoids. When one of them dies, you remove them from the table (or otherwise tag them). When the table has nobody left, you end the match. If the table only has one person remaining, you end the match with your winner.

Below is an example of how you would add every player to a table, as well as remove them from the table upon their death.

local InGame = {}

function Find(Item, Table, Remove)
	for i, v in pairs(Table) do
		if v == Item then
			return Remove and table.remove(Table, i) or Item 
		end
	end
end

for _, Player in pairs(game.Players:GetPlayers()) do
	if Player.Character and Player.Character:FindFirstChild("Humanoid") then
		InGame[#InGame +1] = Player
		Player.Character.Humanoid.Died:connect(function()
			Find(Player, InGame, true) 
		end)
	end
end

1 comment

0

Thanks a lot DataStore!

This discussion is preserved as it was. New answers, comments, and votes are closed.