Easily use GUI with ObjectValues for scripting

This is optional but can be used for simplifying using Gui.

I prefer you use this for server scripts other than the Gui scripts themselves e.g. server scripts located in ServerScriptService or anywhere else.

Because using local scripts could make exploiters cause errors without checks by messing with the values, and potentially cause your scripts to break.

How it will work is by using the

ObjectValue Instance

image

and putting it into a Folder Instance named anything you like for sorting, for example: "ReferenceFolder".
1: image

2: image
Put the Folder in StarterGui, or inside the Gui if it’s elsewhere.
If not, the ObjectValue will reference the original, not the cloned Gui.

Now name your ObjectValue how you want, let’s do "Text1".
image
Then set the Value to your Gui Instance, "TextLabel".
image

You can make a script after that, and do:

local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
    local playerGui = player:FindFirstChild("PlayerGui") -- where all the Gui is

    local ReferenceFolder = playerGui.ReferenceFolder -- location of your Folder
    local Reference1 = ReferenceFolder.Text1.Value -- Your ObjectValue reference
Reference1.Text = "hello"
end

To add more just repeat adding a new ObjectValue to the Folder and do:
image

local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
    local playerGui = player:FindFirstChild("PlayerGui") -- where all the Gui is

    local ReferenceFolder = playerGui.ReferenceFolder -- location of your Folder
    local Reference1 = ReferenceFolder.Text1.Value -- Your ObjectValue reference
    local Reference2 = ReferenceFolder.AnyName.Value -- Your second ObjectValue reference
Reference1.Text = "hello"
-- ...
end

This can help a lot when updating your Gui, this makes it so that you don’t have to change anything for existing scripts when you change the locations of buttons, frames, and other elements…

Without this your script might look like this:

local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
    local playerGui = player:FindFirstChild("PlayerGui") -- where all the Gui is
    local Gui = playerGui.Main
    local Frame = Gui.Frame
    local Buttons = Frame.Buttons
    local TextFrame = Buttons.TextFrame

local TextLabel = TextFrame.Text1
    local TextLabel2 = TextFrame.TextLabel2
-- All of this above ^^^^^^^ would be confusing to edit everytime
TextLabel.Text = "hello"
-- ...
end

or even unsorted:

local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
    local playerGui = player:FindFirstChild("PlayerGui") -- where all the Gui is

local TextLabel = playerGui.Main.Frame.Buttons.TextFrame.TextLabel
 -- you would need to edit this ^^^ everytime the location changes.
TextLabel.Text = "hello"
-- ...
end

If your scripts look like this, you will have to edit every single one of them every time.

So, use it like this when you have Gui that is hard to edit or update, or is too complex,
because with this you can move the TextLabel wherever else within StarterGui and it will work:
image

I’ve tested it and it works perfectly for me and makes me able to edit the Gui easily without needing to edit any scripts.

The only downsides for this is that anyone can see the values changing not just on your client, so only use it if it doesn’t matter.
And it could maybe cause performance issues when too many players are in the game.

3 Likes