如何在 Roblox 中打开和关闭 gui?

2024-01-08

我正在 Roblox 中制作游戏,但遇到了错误。我正在尝试制作在游戏中打开商店的 GUI 按钮。但它打不开。

我试图使按钮不可见而商店可见。一切工作正常,但图形用户界面不会变得可见/不可见。它表示 GUI 在属性中的可见性发生了变化,但在游戏中并未显示出来。我还尝试更改 gui 的父级,它适用于关闭但不适用于打开。

gui = game.StarterGui.ShopSelection
button = game.StarterGui.Shop.Button
button.MouseButton1Down:Connect(function()
    gui.Visible = true
    button.Parent.Visible = false
end)

这应该会在按下 Shop gui 的按钮时打开 ShopSelection gui 并关闭 Shop gui。它不起作用。请帮忙!


您的问题是您正在从以下位置访问该对象StarterGui服务。StarterGui将其内容克隆到播放器的PlayerGui播放器加载后的文件夹。因此,您需要从那里访问该对象。为此,我们将使用LocalScript并通过访问该文件夹LocalPlayer目的。作为注释,LocalScripts只能在玩家的直系后代的地方运行,例如StarterPack, StarterPlayerScripts, StarterCharacterScripts, or StarterGui.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local gui = player:WaitForChild("PlayerGui"):WaitForChild("ShopSelection") --wait for objects
local button = player.PlayerGui:WaitForChild("Shop") --:WaitForChild() yields the thread until the given object is found, so we don't have to wait anymore.

button.MouseButton1Down:Connect(function()
    gui.Visible = true
    button.Visible = false
end)

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Roblox 中打开和关闭 gui? 的相关文章

随机推荐