问题
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