i am trying to make it so when you press a button it makes all the gui not visible. there are no errors. can someone help?
game:GetService("UserInputService").InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.P then game.StarterGui:GetChildren().Visible = false end end)
StarterGui is actually a replication Service—or container. Anything within it is actually transferred to another folder called
PlayerGui, where the authentic GUIs are actually stored, and updated in real-time. To get access to this, we need to reference the
Client (Local Player)
Trying to address the UI from
StarterGui will modify the respective descendant, however, it will not replicate, so for your Script to actually make amendments to the GUI you desire, you need to write the following program below instead, however, there are a few more adjustments to your program that needs to be made.
Firstly, you will most likely encounter another issue regarding line 3, as the metamethod
GetChildren doesn't actually allow you to collectively, and actively manage all the Instances at once. Yet instead, returns a list containing said Instance. Trying to set a Boolean will cause an error.
To fix this, and achieve your goal, you actually have to
iterate through the list, to individually amend the property. This can be done by using a
for loop with the
pairs will feed the loop two variables, commonly known as
i,v. They stand for
Value. The Index is responsible for identifying where you are in the list, whereas the Value is responsible for identifying the Object in respect to the Index; you don't need to understand all that jargon, all we need to know is we only need
v, so we can pass
i to express we don't want it. Finally, pass the list you're working with through
local UserInputService = game:GetService("UserInputService") local Player = game:GetService("Players").LocalPlayer local PlayerGui = Player:WaitForChild("PlayerGui") UserInputService.InputBegan:Connect(function(Input, GameProcessed) if (Input.KeyCode == Enum.KeyCode.P) then for _,v in pairs(PlayerGui:GetChildren()) do if (v:IsA("ScreenGui")) then v.Enabled = false else v.Visible = false end end end end)
Hope this helped! Remember to accept this answer if so!
game:GetService("UserInputService").InputBegan:Connect(function(key) if key.KeyCode == Enum.KeyCode.P then for _,v in pairs(game.StarterGui:GetChildren()) do v.Visible = true end end)