Further enhance and add in an advanced health layering with optimized functions and Fire Frame Events.

Make the code look more complex that even the user confused and hard to understand with optimized/health functions.

Example_Piece-Code:

```
    local m = {Target=nil,Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent()}
	local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
	local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
		CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
	end}
```

Script:

```
-- Local Script in StarterPlayerScripts or StarterCharacterScripts
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Player = LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local InputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BindableEvent = Instance.new("BindableEvent") -- Create a bindable event

-- Create a table to store invincibility frames
local InvincibilityFrames = {
    Duration = 3, -- Duration of invincibility
    Cooldown = 5, -- Cooldown period after invincibility ends
    Active = false
}

-- Metatable for Health Manipulation
local HealthService = {}
HealthService.__index = function(t, k)
    if k == "Health" then
        return Humanoid.Health
    end
    return rawget(t, k)
end

setmetatable(HealthService, HealthService)

-- Function to make the player invincible
local function MakeInvincible()
    if not InvincibilityFrames.Active then
        InvincibilityFrames.Active = true
        Humanoid.MaxHealth = math.huge -- Temporarily set max health to infinite
        Humanoid.Health = math.huge -- Set health to infinite

        -- Emit event to inform other scripts (optional)
        BindableEvent:Fire("InvincibilityActivated")

        wait(InvincibilityFrames.Duration) -- Wait for the duration

        -- Restore health and cooldown
        Humanoid.MaxHealth = 100 -- Reset to normal max health
        Humanoid.Health = 100 -- Reset health to normal

        InvincibilityFrames.Active = false

        -- Emit event to inform other scripts (optional)
        BindableEvent:Fire("InvincibilityDeactivated")
        
        -- Cooldown before the player can become invincible again
        wait(InvincibilityFrames.Cooldown)
    end
end

-- Function to check proximity to other players
local function CheckProximity()
    for _, otherPlayer in ipairs(Players:GetPlayers()) do
        if otherPlayer ~= Player and otherPlayer.Character then
            local otherCharacter = otherPlayer.Character
            local distance = (Character.HumanoidRootPart.Position - otherCharacter.HumanoidRootPart.Position).Magnitude
            if distance <= 10 and not InvincibilityFrames.Active then
                MakeInvincible()
            end
        end
    end
end

-- Input service to detect player interaction (optional customization)
InputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if not gameProcessedEvent then
        -- Example interaction that could be used to trigger invincibility frames
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            CheckProximity() -- Check for proximity on mouse click
        end
    end
end)

-- Loop to constantly check for proximity
while true do
    CheckProximity()
    wait(0.5) -- Check every half second
end