PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Set all Players' Health to 0

Set all Players' Health to 0

Jun 29 2018, 4:52 PM PST

Problem

You want to set all the players’ health to 0 in game.

Solution

You use the Players/GetPlayers method and set the character’s health to 0.

for i, player in ipairs(game.Players:GetPlayers()) do
	if player.Character then
		local hum = player.Character:FindFirstChild('Humanoid')
		if hum then
			hum.Health = 0
		end
	end
end

Discussion

We are passing our iterator function the result of a new function from a whole new directory! This is the Players/GetPlayers function in the game.Players directory. This will return all the in game players.

What is game.Players you may ask? Its just like the Workspace, but instead of where all the rendered parts reside, Player objects reside there. These Player objects contain all the useful information for a Player. The player object contains information like the team color, user ID, and points. It also contains a link to the character model in its Character property.

Why didn’t we just use Players/GetChildren on game.Players, wouldn’t that have the same effect? You can use Players/GetChildren on game.Players, but the difference is that GetPlayers will get all the Players (e.g. if there are objects that are not players in game.Players, it won’t select them), however GetChildren will select all children regardless of whether they are actually a Player or not. Normally you will only have Players in this directory, however if something does get in there, its safer to use GetPlayers.

Inside the loop we first check if the character is there (the character might not be there if the player is respawning) and then safely index the Humanoid from Player.Character, check if its exists, and then set its health to 0.

Tags:
  • mechanic
  • rounds