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

how to make team B wins if team A dead?

Suppose this is the situation: team B killed the entire team A, and now in Team A the number of players is zero. Team B wins and everyone teleports in the lobby.

3 comments

0

You should show us an example since we could tell you a script which wouldn't work with your game if you had anything additional in it.

0

Make a script that checks every player on each teams health and if all of the players on a certain team have died then the other team is declared the winner.

0

make 3 teams, team A, team B, and lobby (or spawn), and when they die, make them move to lobby. then check if the amount of players in team A/B == 0, make the standing team win

1 answer

✓ Accepted answer

This script is going off of the assumption that you are using folders to keep track of what players are alive via models that are named the same as the player. If this isn't true then it should still give you an idea.

The humanoid of a character has an event known as Died. If you connect this then the script you wrote in it will run every time the character dies. Lets connect that like this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()

		end)
	end)
end)

What that script does in order can be shown in this numbered list:

1.) Runs when a player joins the game 2.) Runs once the character of that player is added to the workspace 3.) Waits for the character's humanoid and connects the Died event

Next what we need to do is specify what we want the script to do when the player dies. In our case, we want their name to be removed from the folder, and if 0 players are alive then we want to end the round. Lets do that now:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			if game.ServerStorage.TeamA:FindFirstChild(plr.Name) then
				game.ServerStorage.TeamA[plr.Name]:Destroy()
			elseif game.ServerStorage.TeamB:FindFirstChild(plr.Name) then
				game.ServerStorage.TeamB[plr.Name]:Destroy()
			end

			if #game.ServerStorage.TeamA:GetChildren() == 0 then
				-- end round with TeamB as the winner
			elseif #game.ServerStorage.TeamB:GetChildren == 0 then
				-- end round with TeamA as the winner
			end
		end)
	end)
end)

This script works fine, but thats only assuming that nobody leaves mid round. Lets implement a fix for that by connecting the PlayerRemoving event.

game.Players.PlayerRemoving:Connect(function(plr)
	if game.ServerStorage.TeamA:FindFirstChild(plr.Name) then
		game.ServerStorage.TeamA[plr.Name]:Destroy()
	elseif game.ServerStorage.TeamB:FindFirstChild(plr.Name) then
		game.ServerStorage.TeamB[plr.Name]:Destroy()
	end
	
	if #game.ServerStorage.TeamA:GetChildren() == 0 then
		-- end round with TeamB as the winner
	elseif #game.ServerStorage.TeamB:GetChildren == 0 then
		-- end round with TeamA as the winner
	end
end)

I hope this helped you out, and if you have any questions then make sure to comment them on this answer.

1 comment

0

Thanks!

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