Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to decrease all players health?

Asked by 3 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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
Ad
Log in to vote
1
Answered by 3 years ago

Hey! I can't write scripts for you but, I reccomend looking at https://developer.roblox.com/en-us/api-reference/property/Humanoid/Health and https://blog.roblox.com/2012/05/using-wait-wisely/

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
3 years ago
Edited 3 years ago

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

Answer this question