Help to reference the Text property of a Textlabel

I am quite new to scripting so I apologize if I do any simple mistakes.

I am trying to make a text label that will update, and I want to refrence it’s text property by using a variable. I tried doing:

local textVariable = game.StarterGui.ScreenGui.TextLabel.Text 

textVariable = "test"

It did not work what so ever with the variable, however if I only refrenced the property without using a variable it worked. The code that worked is showed under:

game.StarterGui.ScreenGui.TextLabel.Text = "test"

I was wondering if anyone could teach me how I could reference the property with a variable so I didn’t have to type out such a long pathway every time.

Thank you for your time!

2 Likes

You’re referencing the property correctly. However, you should not be using game.StarterGui. Instead, get the local player’s PlayerGui. The reason for this is because everything in the StarterGui gets copied into the PlayerGui.

Example code:

-- this would be in a local script in startergui

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui") -- the PlayerGui is located directly inside the player object
local textLabel = playerGui.ScreenGui.TextLabel

textLabel.Text = "This is an example."
1 Like

Thank you for the explanation.

1 Like