How do you open and close a gui in Roblox?

后端 未结 1 1585
情话喂你
情话喂你 2021-01-28 07:12

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 but

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 07:40

    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!

    0 讨论(0)
提交回复
热议问题