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

How can I reference items StarterGui?

So I want to be able to reference an image label but it's not working. Someone help.

Here is my code:

local imageLabel = game.StarterGui.ScreenGui.ImageLabel

local UserInputSerivce = game:GetService("UserInputService")

UserInputSerivce.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.M then
		imageLabel.Visible = true
	end
end)

1 answer

✓ Accepted answer

Note: StarterGui waits until a player joins, clones all of its contents, and moves all the cloned UI objects to the PlayerGui in the player object(This applies to all players in the server).

  1. Insert a LocalScript into StarterPlayerScripts located in StarterPlayer.

  2. In the script, write the following code:

local UserInputService = game:GetService("UserInputService") -- getting UserInputService
local playerGui = game.Players.LocalPlayer.PlayerGui -- defining PlayerGui
local imageLabel = playerGui:WaitForChild("ScreenGui"):WaitForChild("ImageLabel") -- make sure to define your ImageLabel because I don't know what your GUI hierarchy looks like. Just keep doing :WaitForChild() until you reach ImageLabel


UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    -- gameProcessedEvent is basically used to check to see if the player is interferring with the engine EXAMPLE: The chat
    if input.KeyCode == Enum.KeyCode.M and not gameProcessedEvent then -- checks whether the player chatting

        imageLabel.Visible = true
    end
end)

If you're having problems just reply and you'll get a response within 2 hours.

1 comment

0

Thank U so much it worked!!!

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