Further enhance and add in an advanced invicibility frames buffer with health layering. (For my invincibility frames script)

```
local Services = setmetatable({}, {__index = function(t, k) return game:GetService(k) end})
local P = Services.Players.LocalPlayer
local C = P.Character or P.CharacterAdded:Wait()
local H = C:WaitForChild("Humanoid")
local UIS = Services.UserInputService
local RS = Services.ReplicatedStorage
local BE = Instance.new("BindableEvent")

local IFM = setmetatable({
    D = 3, 
    CD = 5, 
    A = false, 
    E = {Activate = BE.Event, Deactivate = BE.Event}
}, {
    __index = function(t, k)
        return rawget(t, k)
    end
})

local HS = setmetatable({}, {
    __index = function(t, k)
        if k == "H" then
            return H.Health
        elseif k == "MH" then
            return H.MaxHealth
        end
        return rawget(t, k)
    end,
    __newindex = function(t, k, v)
        if k == "H" then
            H.Health = v
        elseif k == "MH" then
            H.MaxHealth = v
        else
            rawset(t, k, v)
        end
    end
})

local function ToggleInvincibility()
    if not IFM.A then
        IFM.A = true
        HS.MH = math.huge
        HS.H = math.huge
        IFM.E.Activate:Fire("InvincibilityEnabled")

        coroutine.wrap(function()
            wait(IFM.D)
            HS.MH = 100
            HS.H = 100
            IFM.A = false
            IFM.E.Deactivate:Fire("InvincibilityDisabled")
            wait(IFM.CD)
        end)()
    end
end

local function ProximityCheck()
    local CP = C:FindFirstChild("HumanoidRootPart")
    if not CP then return end
    local pos = CP.Position
    for _, OP in ipairs(Services.Players:GetPlayers()) do
        if OP ~= P and OP.Character then
            local OCP = OP.Character:FindFirstChild("HumanoidRootPart")
            if OCP and (pos - OCP.Position).Magnitude <= 10 and not IFM.A then
                ToggleInvincibility()
                break
            end
        end
    end
end

local function InputHandler()
    UIS.InputBegan:Connect(function(input, gp)
        if not gp then
            if input.UserInputType == Enum.UserInputType.MouseButton1 then
                ProximityCheck()
            end
        end
    end)
end

Services.RunService.Heartbeat:Connect(function()
    ProximityCheck()
end)

coroutine.wrap(function()
    InputHandler()
    while true do
        ProximityCheck()
        wait(0.5)
    end
end)()

local StatusEffects = setmetatable({}, {
    __index = function(t, k)
        return rawget(t, k) or {}
    end
})

local function ApplyStatusEffect(player, effect, duration)
    table.insert(StatusEffects[player.Name], {Effect = effect, Time = os.time() + duration})

    BE:Fire("StatusEffectApplied", player, effect)
end

local function RemoveExpiredEffects()
    for _, player in ipairs(Services.Players:GetPlayers()) do
        local effects = StatusEffects[player.Name]
        if effects then
            for i = #effects, 1, -1 do
                if effects[i].Time <= os.time() then
                    table.remove(effects, i)

                    BE:Fire("StatusEffectRemoved", player, effects[i].Effect)
                end
            end
        end
    end
end

coroutine.wrap(function()
    while true do
        RemoveExpiredEffects()
        wait(1)
    end
end)()

BE.Event:Connect(function(eventName)
    if eventName == "InvincibilityActivated" then
        ApplyStatusEffect(P, "Shield", 5)
    end
end)