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

Roblox LocalScript按钮点击关闭其他Frame功能失效求助

Roblox LocalScript 按钮关闭Frame功能失效修复方案

核心问题在于你直接操作了StarterGui中的GUI元素——Roblox里StarterGui只是界面模板,游戏启动后会自动把里面的内容复制到玩家的PlayerGui文件夹中,玩家实际看到并交互的是PlayerGui里的副本。修改StarterGui的元素不会对玩家当前界面产生任何影响,这就是脚本功能失效的根本原因。

修正后的代码如下:

local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")

-- 改为引用PlayerGui中的实际界面元素
local DonationGui = PlayerGui:WaitForChild("GamepassesGUI").Box
local TutorialGui = PlayerGui:WaitForChild("TutorialGUI").Box
local CashDropGui = PlayerGui:WaitForChild("CashDropGui").MainFrame
local currentBox = script.Parent.Parent.Box

local function checkAnyGuiOpen(...)
    for _, gui in ipairs({...}) do
        if gui.Visible then
            return true
        end
    end
    return false
end

local function closeAllGuis(excludeGui, ...)
    for _, gui in ipairs({...}) do
        if gui ~= excludeGui then
            gui.Visible = false
        end
    end
end

script.Parent.MouseButton1Click:Connect(function()
    local hasOpenGui = checkAnyGuiOpen(DonationGui, TutorialGui, CashDropGui)
    
    -- 关闭其他GUI(排除当前要切换的Box)
    closeAllGuis(currentBox, DonationGui, TutorialGui, CashDropGui)
    -- 额外关闭Currency.Frame
    local currencyFrame = PlayerGui:WaitForChild("Currency").Frame
    currencyFrame.Visible = false
    
    -- 切换当前Box的显示状态
    currentBox.Visible = not currentBox.Visible
end)

关键改动说明

  • 替换StarterGui为PlayerGui:确保操作的是玩家实际可见的界面副本
  • 简化逻辑:把重复的toggle逻辑合并为currentBox.Visible = not currentBox.Visible,减少冗余代码
  • 新增排除逻辑:关闭其他GUI时跳过当前要切换的Box,避免不必要的状态修改
  • 优化检查函数:改用可变参数,后续新增GUI时不需要修改函数结构

内容的提问来源于stack exchange,提问作者Brody Simpson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:42:18