Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

userinputservice not working no errors?

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)

2 comments

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

0

Also in case im wrong ill put a solution in answers

2 answers

✓ 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!

5 comments

0

You forgot "do" Apparently making a mistake is good enough for a downvote

0

LocalScript (7, 12): Expected 'do', got 'Gui' i feel like it gonna work but can you edit?

0

Apologies, I made the same mistake;)

0

line 9 visible is supposed to be enabled so i did that and now it works

0

oof i forgot to accept

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)

7 comments

0

Explanation: Trying to define a table won't do anything.

0

Ask if you still have questions ill try to answer it.

0

Expected 'do', got 'v'

0

oh thats not my downvote tho it someone elses

0

Imma just give people free answers and thats about it

0

I didn't downvote this either

0

i upvoted so it doesnt show downvote

This discussion is preserved as it was. New answers, comments, and votes are closed.