Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

userinputservice not working no errors?

Asked by
3wdo 198
5 years 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 148 — 5y
0
Also in case im wrong ill put a solution in answers voidofdeathfire 148 — 5y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years 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 148 — 5y
0
LocalScript (7, 12): Expected 'do', got 'Gui' i feel like it gonna work but can you edit? 3wdo 198 — 5y
0
Apologies, I made the same mistake;) Ziffixture 6913 — 5y
0
line 9 visible is supposed to be enabled so i did that and now it works 3wdo 198 — 5y
0
oof i forgot to accept 3wdo 198 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years 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 148 — 5y
0
Ask if you still have questions ill try to answer it. voidofdeathfire 148 — 5y
0
Expected 'do', got 'v' 3wdo 198 — 5y
0
oh thats not my downvote tho it someone elses 3wdo 198 — 5y
View all comments (3 more)
0
Imma just give people free answers and thats about it voidofdeathfire 148 — 5y
0
I didn't downvote this either Ziffixture 6913 — 5y
0
i upvoted so it doesnt show downvote 3wdo 198 — 5y

Answer this question