How do you open and close a gui in Roblox?

若如初见. 提交于 2019-12-13 20:27:27

问题


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.

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)

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!


回答1:


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)

Hope this helps!



来源:https://stackoverflow.com/questions/55676724/how-do-you-open-and-close-a-gui-in-roblox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!