You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Lua 开发:如何实现F键切换E/R按键触发通知的开关功能

实现方案

核心思路是新增一个状态变量控制通知的显隐逻辑,步骤如下:

  • 新增布尔类型的开关变量,默认值为true(代表默认允许弹出通知)
  • 新增F键的按下监听逻辑,每次按下F键翻转开关的状态
  • 原有E/R键的通知弹出逻辑前新增开关校验,仅开关为开启状态时才执行通知弹出
修改后完整代码
-- 按键绑定定义
local KeyBindHigh = "e"
local KeyBindLow = "r"
local KeyBindToggle = "f"

-- 通知开关状态,默认开启
local notificationsEnabled = true
-- 请根据实际需求补充这两个变量的初始值,原代码未定义会报错
local size = 0
local change = 1

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(Key)
    -- 通知开关逻辑
    if Key == KeyBindToggle then
        notificationsEnabled = not notificationsEnabled
        -- 按下F键时提示当前开关状态,不需要可以删掉这段
        game.StarterGui:SetCore("SendNotification", {
            Title = "~ BlackRose ~";
            Text = notificationsEnabled and "通知已开启" or "通知已关闭";
            Icon = "";
            Duration = 1;
        })
    end

    -- 调高逻辑
    if Key == KeyBindHigh then 
        size = size + change
        -- 仅开关开启时弹出通知
        if notificationsEnabled then
            game.StarterGui:SetCore("SendNotification", {
                Title = "~ BlackRose ~";
                Text = "Reach set to " .. size;
                Icon = "";
                Duration = 1;
            })
        end
    end

    -- 调低逻辑
    if Key == KeyBindLow then 
        size = size - change
        -- 仅开关开启时弹出通知
        if notificationsEnabled then
            game.StarterGui:SetCore("SendNotification", {
                Title = "~ BlackRose ~";
                Text = "Reach set to " .. size;
                Icon = "";
                Duration = 1;
            })
        end
    end
end)
逻辑说明
  • 如果你需要按下F键后连E/R的数值调整功能也一起禁用,把size = size ± change的逻辑也移到notificationsEnabled的判断分支内即可
  • 按下F键的状态提示可以根据你的需求删除,不影响核心开关逻辑
  • size和change的初始值请根据你的业务需求自行调整

内容的提问来源于stack exchange,提问作者Santiago Uribe Gil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 17:36:02