您如何在Roblox中打开和关闭GUI?

时间:2019-04-14 15:06:45

标签: roblox

我正在用Roblox做游戏,但遇到错误。我正在尝试使gui按钮在游戏中打开商店。但是它没有打开。

我试图使按钮不可见并且商店可见。一切正常,但guis变得不可见/不可见。它说改变了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。它不起作用。请帮忙!

1 个答案:

答案 0 :(得分:0)

您的问题是您正在从StarterGui服务访问该对象。播放器加载后,StarterGui会将其内容克隆到播放器的PlayerGui文件夹中。因此,您需要从那里访问对象。为此,我们将使用LocalScript并通过LocalPlayer对象访问该文件夹。请注意,LocalScripts只能在玩家直接后代的地方运行,例如StarterPackStarterPlayerScriptsStarterCharacterScriptsStarterGui。 / p>

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)

希望这会有所帮助!

相关问题