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

Which of those ways is more effective perfomance-wise to make a health regeneration script?

Asked by 3 years ago

I've been working on a health regeneration script to work alongside the default roblox health regeneration script, but it works slightly differently, it only is active when the player's health is below 50%. It is pretty simple and is working completely fine, I just wanted to know if using Humanoid.HealthChanged is a problem considering that the function would fire whenever the health value changed, so it would fire not only when the player takes damage, but also when they are healed. I realized that the default health regen uses "while Humanoid.Health > 0 do" instead of using HealthChanged function, but as it is a loop which is always running for as long as the player is alive so I'm worried it may cause lag. Basically my question is which of those should I use? Which would be better perfomance-wise?

The one I'm currently using:

Humanoid.HealthChanged:Connect(function()
    if Humanoid.Health < Humanoid.MaxHealth / 2 and Active.Value == false then
            Active.Value = true

            while Humanoid.Health < Humanoid.MaxHealth / 2 do
                wait(RegenerationDelay)
                Humanoid.Health = Humanoid.Health + RegenerationRate
            end

            Active.Value = false
    end
end)

The one I could use:

while Humanoid.Health > 0 do
    if Humanoid.Health < Humanoid.MaxHealth / 2 and Active.Value == false then
        Active.Value = true

        while Humanoid.Health < Humanoid.MaxHealth / 2 do
            wait(RegenerationDelay)
            Humanoid.Health = Humanoid.Health + RegenerationRate
        end

        Active.Value = false
    end

end

0
You would have to have an insanely large game for either of these to cause problems with lag (someone can correct me if I'm wrong about that). Naturally the one that runs less would be connecting the event and it would therefore cause less lag. cmgtotalyawesome 1418 — 3y

Answer this question