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

How to decrease all players health?

How would I SLOWLY decrease all players health?

I know I would use

i,v in pairs do()

and change all humanoids

 .Health 

value. But I don't know how to go about that.

3 answers

✓ Accepted answer

As you knew, iterate through each player in the game via the :GetPlayers() function of Players.

Allocate their Characters' Humanoid Instance, then begin decreasing their health through a loop.

####You must do this on a separate thread, as it will yield the pairs iterator from continuing forward with initiating the other Player's health decrement.

local Players = game:GetService("Players")


for _, Player in pairs(Players:GetPlayers()) do
	local Character = Player.Character
	if (Character) then
		---------------
		local Humanoid = Character.Humanoid
		---------------
		spawn(function()
			---------------
			while (Humanoid.Health > 0) do
				Humanoid.Health -= 1
				---------------
				wait(--[[Time]])
			end
		end)
	end
end

Use a for loop and loop through the array of players using :GetPlayers(). Keep in mind you must use a different thread for each player, which can be done using spawn, coroutine.wrap, or coroutine.resume(coroutine.create()).

for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
	local character = player.Character

	if not character then
		continue
	end

	local humanoid = character.Humanoid

	coroutine.wrap(function()
		while humanoid and humanoid.Health > 0 do
			humanoid.Health -= 1

			wait(n)
		end
	end)()
end

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