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

Preventing Health Regeneration? [Solved]

Asked by 10 years ago

I'm working on limiting players' health in my game to prevent it from regenerating over time by default. Is there an easy method or property I can change to do this, or will I have to write some crazy script that records the current health and manually keeps it at that level?

1 answer

Log in to vote
4
Answered by
Link43758 175
10 years ago

There is a script in every player named 'Health' that manages regeneration, shown below:

--Responsible for regening a player's humanoid's health

-- declarations
local Figure = script.Parent
local Head = Figure:WaitForChild("Head")
local Humanoid = Figure:WaitForChild("Humanoid")
local regening = false

-- regeneration
function regenHealth()
    if regening then return end
    regening = true

    while Humanoid.Health < Humanoid.MaxHealth do
        local s = wait(1)
        local health = Humanoid.Health
        if health > 0 and health < Humanoid.MaxHealth then
            local newHealthDelta = 0.01 * s * Humanoid.MaxHealth
            health = health + newHealthDelta
            Humanoid.Health = math.min(health,Humanoid.MaxHealth)
        end
    end

    if Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end

    regening = false
end

Humanoid.HealthChanged:connect(regenHealth)

All you need to do is delete this script when a player respawns or spawns! To do this, insert this into a LocalScript in StarterGui:

game.Players.LocalPlayer.Character.Health:Destroy()

No crazy scripting involved! Hope I've helped! If you need any help, please comment, I'd be glad to fix your script or answer any questions.

0
Ahh, ok thanks, and messing with that doesn't have any other strings attached to worry about? deaththerapy 60 — 10y
0
None at all, but you should know that it's better practice to use Debris:AddItem() to destroy members of spawning Characters, as they have a tendency to not actually get destroyed otherwise. adark 5487 — 10y
Ad

Answer this question