_G.CloneClass(IngameWaitingForPlayersState)
function IngameWaitingForPlayersState:at_exit(...)
    IngameWaitingForPlayersState.orig.at_exit(self, ...)


    HT.debugLogClass.allow_new_entries = false

    local function apply(fn)
        local ok, err = pcall(fn)
        if not ok then
            pcall(function() HT.debugLogClass:log("Re-apply failed: " .. tostring(err)) end)
        end
    end

    apply(function() HT.Dexterity:setGodMode(HT:getSetting("enable_god_mode"), false) end)
    apply(function() HT.Dexterity:setInfiniteStamina(HT:getSetting("enable_infinite_stamina")) end)
    apply(function() HT.Dexterity:setRunInAllDirections(HT:getSetting("enable_run_in_all_directions")) end)
    apply(function() HT.Dexterity:setCanRunWithAnyBag(HT:getSetting("enable_can_run_with_any_bag")) end)
    apply(function() HT.Dexterity:setFastMask(HT:getSetting("enable_fast_mask")) end)
    apply(function() HT.Dexterity:setNoCarryCooldown(HT:getSetting("enable_no_carry_cooldown")) end)
    apply(function() HT.Dexterity:setNoFlashbangs(HT:getSetting("enable_no_flashbangs")) end)
    apply(function() HT.Dexterity:setInstantSwap(HT:getSetting("enable_instant_swap")) end)
    apply(function() HT.Dexterity:setInstantReload(HT:getSetting("enable_instant_reload")) end)
    apply(function() HT.Dexterity:setShootThroughWalls(HT:getSetting("enable_shoot_through_walls"), false) end)
    apply(function() HT.Dexterity:setNoRecoil(HT:getSetting("enable_no_recoil")) end)
    apply(function() HT.Dexterity:setNoSpread(HT:getSetting("enable_no_spread")) end)
    apply(function() HT.Dexterity:setUnlimitedAmmo(HT:getSetting("enable_unlimited_ammo")) end)
    apply(function() HT.Dexterity:setInstantInteraction(HT:getSetting("enable_instant_interaction")) end)
    apply(function() HT.Dexterity:setInstantDeployment(HT:getSetting("enable_instant_deployment")) end)
    apply(function() HT.Dexterity:setUnlimitedEquipment(HT:getSetting("enable_unlimited_equipment")) end)
    apply(function() HT.Dexterity:setMoveSpeedMultiplier(HT:getSetting("enable_move_speed_multiplier"), HT:getSetting("move_speed_multiplier")) end)
    apply(function() HT.Dexterity:setThrowDistanceMultiplier(HT:getSetting("enable_throw_distance_multiplier"), HT:getSetting("throw_distance_multiplier")) end)
    apply(function() HT.Dexterity:setFireRateMultiplier(HT:getSetting("enable_fire_rate_multiplier"), HT:getSetting("fire_rate_multiplier")) end)
    apply(function() HT.Dexterity:setDamageMultiplier(HT:getSetting("enable_damage_multiplier"), HT:getSetting("damage_multiplier")) end)
    apply(function() HT.Dexterity:setNoFallDamage(HT:getSetting("enable_no_fall_damage")) end)
    apply(function() HT.Dexterity:setMeleeRange(HT:getSetting("enable_melee_range")) end)
    apply(function() HT.Dexterity:setMeleeDamageMultiplier(HT:getSetting("enable_melee_damage_multiplier"), HT:getSetting("melee_damage_multiplier") or 5, false) end)
    apply(function() HT.Dexterity:setOneShotKill(HT:getSetting("enable_one_shot_kill")) end)
    apply(function()
        HT.Dexterity.noclipSpeedMultiplier = tonumber(HT:getSetting("noclip_speed_multiplier")) or 2
        HT.Dexterity:setNoclip(HT:getSetting("enable_noclip") and true or false, true, false)
    end)

    apply(function() HT.Mission:setDisableAi(HT:getSetting("enable_disable_ai") and true or false) end)
    apply(function() HT.Mission:setInvisiblePlayer(HT:getSetting("enable_invisible_player") and true or false) end)
    apply(function() HT.Mission:setInstantConvert(HT:getSetting("enable_instant_convert") and true or false) end)
    apply(function() HT.Mission:setInstantDrilling(HT:getSetting("enable_instant_drilling") and true or false) end)
    apply(function() HT.Mission:setPreventAlarmTriggering(HT:getSetting("enable_prevent_alarm_triggering") and true or false) end)
    apply(function() HT.Mission:setUnlimitedPagers(HT:getSetting("enable_unlimited_pagers") and true or false) end)
    apply(function() HT.Mission:setXray(HT:getSetting("enable_xray") and true or false) end)
    apply(function() HT.Mission:setFreezeCivilians(HT:getSetting("enable_freeze_civilians") and true or false) end)

    -- Re-apply the anti-cheat bypass now that the networking classes (NetworkPeer
    -- etc.) are fully loaded. Applying only at boot can silently miss them, which
    -- is why the "cheated by picking up too many bags" message could still fire
    -- even as host with the bypass enabled.
    apply(function() if HT.AntiCheat and HT.AntiCheat.restore then HT.AntiCheat:restore() end end)
    apply(function() if HT.Extra and HT.Extra.reapply then HT.Extra:reapply() end end)
    -- Re-apply Team Menu protections for the newly loaded heist.
    apply(function() if HT.TrollMenu and HT.TrollMenu.reapply then HT.TrollMenu:reapply() end end)
    -- Tools: re-apply name spoof + team instant interaction.
    apply(function() if HT.Misc then HT.Misc:reapplyNameSpoof(); HT.Misc:reapplyInteractForTeam() end end)
    -- Always-on crash guard for instant bag pickups (see Misc:installCarrySafety).
    apply(function() if HT.Misc then HT.Misc:installCarrySafety() end end)
    -- Re-apply persistent toggles too: some of their classes (BaseNetworkSession
    -- for No Pause, etc.) aren't loaded yet at boot, so apply them again here.
    apply(function() if HT.Extra and HT.Extra.reapplyPersistent then HT.Extra:reapplyPersistent() end end)

    HT.debugLogClass.allow_new_entries = true
end
