New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

userinputservice not working no errors?

Asked by 4 months ago

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)
0
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 voidofdeathfire 126 — 4mo
0
Also in case im wrong ill put a solution in answers voidofdeathfire 126 — 4mo

2 answers

Log in to vote
1
Answered by
Feahren 1859 Moderation Voter
4 months ago
Edited 4 months ago

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!

0
You forgot "do" Apparently making a mistake is good enough for a downvote voidofdeathfire 126 — 4mo
0
LocalScript (7, 12): Expected 'do', got 'Gui' i feel like it gonna work but can you edit? AlbertoMiAmigo2 166 — 4mo
0
Apologies, I made the same mistake;) Feahren 1859 — 4mo
0
line 9 visible is supposed to be enabled so i did that and now it works AlbertoMiAmigo2 166 — 4mo
0
oof i forgot to accept AlbertoMiAmigo2 166 — 4mo
Ad
Log in to vote
0
Answered by 4 months ago
Edited 4 months ago
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)
0
Explanation: Trying to define a table won't do anything. voidofdeathfire 126 — 4mo
0
Ask if you still have questions ill try to answer it. voidofdeathfire 126 — 4mo
0
Expected 'do', got 'v' AlbertoMiAmigo2 166 — 4mo
0
oh thats not my downvote tho it someone elses AlbertoMiAmigo2 166 — 4mo
View all comments (3 more)
0
Imma just give people free answers and thats about it voidofdeathfire 126 — 4mo
0
I didn't downvote this either Feahren 1859 — 4mo
0
i upvoted so it doesnt show downvote AlbertoMiAmigo2 166 — 4mo

Answer this question