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

How can I reference items StarterGui?

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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.

0
Thank U so much it worked!!! II_Goodlu 8 — 3y
Ad

Answer this question