如何在 Roblox 中打开和关闭 gui? [英] How do you open and close a gui in Roblox?

查看:86
本文介绍了如何在 Roblox 中打开和关闭 gui?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Roblox 中制作游戏时遇到错误.我正在尝试制作在游戏中打开商店的 gui 按钮.但是打不开.

I am making a game in Roblox and I came across an error. I am trying to make gui button that opens the shop in the game. But it does not open.

我试图让按钮不可见而商店可见.一切正常,但 guis 不会变得可见/不可见.它说明了 gui 在属性中的可见性的变化,但它没有在游戏中显示出来.我也尝试更改 gui 的父级,它适用于关闭但无法打开.

I'v tried to make the button not visible and the shop visible. Everything is workig fine but the guis do not become visible/invisible. It says the change to the gui's visibility in the proproties, but it does not show it in the game. I also tryed to change the gui's parent, it works for closing but not opening.

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

这应该是打开 ShopSelection gui 并在按下 Shop gui 的按钮时关闭 Shop gui.它不工作.请帮忙!

This is supposed to open the ShopSelection gui and close the Shop gui when the Shop gui's button is pressed. It is not working. Please help!

推荐答案

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

Your issue is that you're accessing the object from the StarterGui service. StarterGui clones its contents into the player's PlayerGui folder once the player loads. Thus, you need to access the object from there. To do this, we'll use a LocalScript and access the folder through the LocalPlayer object. As a note, LocalScripts can only run in places, which are direct descendants of the player, like 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)

希望这会有所帮助!

这篇关于如何在 Roblox 中打开和关闭 gui?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆