✓ Accepted answer
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 function. pairs will feed the loop two variables, commonly known as i,v. They stand for Index and 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 _ at i to express we don't want it. Finally, pass the list you're working with through pairs.
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!
2 comments
This wouldn't work for a lot of reasons. Main one is that you are trying to get children of startergui which is most likely a screengui which does not have the property visible
Also in case im wrong ill put a solution in answers