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

How can I reference an item when it is in multiple parents?

Asked by 3 years ago

I didn't know what to name the title but basically here is my code and I want to reference a text label but it's on StarterGui, MenuGui, Frame, Text Button. Here is my code.

local textButton = script.Parent
local textLabel = game.Workspace.StarterGui.MenuGui.Frame.TextButton.textLabel
textButton.MouseButton1Click:Connect(function()
    textLabel.Visible = true
end)

0
Technically speaking, it doesn't have multiple parents. It has multiple ancestors. appxritixn 2235 — 3y

1 answer

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

A couple of things to list right off the bat:

1. StarterGui is a service and not a member of game.Workspace

2. The version of the ScreenGui that you are referencing in StarterGui will not be changed. No players have direct access to the version in StarterGui. The correct method would be accessing the GUI from the Player's PlayerGui folder (a child of the Player instance in "Players" service)

LocalScript:

local textButton = script.Parent
local Player = game:GetService("Players").LocalPlayer
local textLabel = Player.PlayerGui.MenuGui.Frame.TextButton.textLabel 
-- If textLabel is a child of the textButton variable, you could also do
-- local textLabel = textButton:WaitForChild("textLabel")
textButton.MouseButton1Click:Connect(function()
    textLabel.Visible = true
end)

I would also recommend doing checks to make sure each element you are referencing actually exists to prevent nil errors.

0
Thank You II_Goodlu 8 — 3y
0
Wait it still didn't work. II_Goodlu 8 — 3y
1
Do you get any errors? appxritixn 2235 — 3y
0
No. II_Goodlu 8 — 3y
View all comments (2 more)
1
When you hover over the button, does its background change colors automatically? If not, I would suggest increasing the TextButton's Z-index appxritixn 2235 — 3y
0
Can anyone help me. II_Goodlu 8 — 3y
Ad

Answer this question