Line 39: attempt to call a nil value:

```
-- Advanced Invincibility Script

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

-- Create a fake health value
local FakeHealth = Instance.new("NumberValue")
FakeHealth.Value = Humanoid.Health

-- Function to update the fake health
local function updateFakeHealth()
    FakeHealth.Value = math.clamp(FakeHealth.Value, 0, Humanoid.MaxHealth)
end

-- Create a proxy table for the Humanoid
local HumanoidProxy = setmetatable({}, {
    __index = function(_, k)
        if k == "Health" then
            return FakeHealth.Value
        end
        return Humanoid[k]
    end,
    __newindex = function(_, k, v)
        if k == "Health" then
            FakeHealth.Value = v
            updateFakeHealth()
        else
            Humanoid[k] = v
        end
    end
})

-- Replace references to Humanoid with HumanoidProxy
local function replaceHumanoidReferences()
    for _, v in pairs(getgc(true)) do
        if type(v) == "table" and rawget(v, "Humanoid") == Humanoid then
            v.Humanoid = HumanoidProxy
        end
    end
end

-- Handle damage simulation
local function simulateDamage()
    Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
    task.wait(0.1)
    Humanoid:ChangeState(Enum.HumanoidStateType.Running)
end

-- Connect to HealthChanged event
local healthChangedConnection
healthChangedConnection = Humanoid.HealthChanged:Connect(function(health)
    if health < FakeHealth.Value then
        -- Damage taken
        local damageTaken = FakeHealth.Value - health
        FakeHealth.Value = FakeHealth.Value - damageTaken
        updateFakeHealth()
        simulateDamage()
        Humanoid.Health = Humanoid.MaxHealth  -- Restore real health
    elseif health > FakeHealth.Value then
        -- Health increased (e.g., healing)
        FakeHealth.Value = health
        updateFakeHealth()
    end
end)

-- Create a table to store original properties
local originalProperties = {}

-- Function to save original properties
local function saveOriginalProperties(instance)
    originalProperties[instance] = {}
    for _, prop in ipairs({"Health", "MaxHealth"}) do
        pcall(function()
            originalProperties[instance][prop] = instance[prop]
        end)
    end
end

-- Function to restore original properties
local function restoreOriginalProperties(instance)
    if originalProperties[instance] then
        for prop, value in pairs(originalProperties[instance]) do
            pcall(function()
                instance[prop] = value
            end)
        end
    end
end

-- Save original Humanoid properties
saveOriginalProperties(Humanoid)

-- Hook to intercept property changes
local function hookProperty(instance, property)
    local success, value = pcall(function()
        return instance[property]
    end)
    
    if success then
        instance:GetPropertyChangedSignal(property):Connect(function()
            if property == "Health" then
                FakeHealth.Value = instance[property]
                updateFakeHealth()
                instance[property] = originalProperties[instance][property]
            elseif property == "MaxHealth" then
                FakeHealth.Value = math.min(FakeHealth.Value, instance[property])
                updateFakeHealth()
            end
        end)
    end
end

-- Hook Humanoid properties
hookProperty(Humanoid, "Health")
hookProperty(Humanoid, "MaxHealth")

-- Constantly update real health to max
RunService.Heartbeat:Connect(function()
    Humanoid.Health = Humanoid.MaxHealth
end)

-- Function to clean up and restore original state
local function cleanup()
    restoreOriginalProperties(Humanoid)
    if healthChangedConnection then
        healthChangedConnection:Disconnect()
    end
    FakeHealth:Destroy()
end

-- Clean up when the character dies or is removed
Humanoid.Died:Connect(cleanup)
Character.AncestryChanged:Connect(function(_, parent)
    if not parent then
        cleanup()
    end
end)

-- Replace Humanoid references
replaceHumanoidReferences()

print("Advanced invincibility script loaded successfully!")