Here is the structure of my work so far.
I have an ImageButton I'm using in the main menu on my game. When I change the properties by writing, for example:
game.StarterGui.ScreenGui.MainMenuFrame.MainMenuAboutButton.x = 0.5
or
script.Parent.x = 0.5
where x equals a property, the code works and changes the properties.
However, when I define the ImageButton as a variable like this:
aboutButton = game.StarterGui.ScreenGui.MainMenuFrame.MainMenuAboutButton
and then try to change the properties like:
aboutButton.x = 0.5
nothing happens. It seems like the code doesn't even consider the variable as the button I've defined it as, because I have an event that links to it that doesn't even fire but does if I use script.Parent.
Sorry if this is obvious, thank you!
You mustn't use StarterGui
for this purpose. It's only used to store templates of guis which are then copied to the player's PlayerGui
.
You should use PlayerGui
instead.
local aboutButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.MainMenuFrame.MainMenuAboutButton aboutButton.x = 0.5
According to ROBLOX Wiki:
The PlayerGui object is a container that holds a Player's user GUI. If a ScreenGui is a descendant of a PlayerGui, then any GuiObject inside of the ScreenGui will be drawn to the player's screen. Any LocalScript will run as soon as it is inserted into a PlayerGui. When a player first joins a game, their PlayerGui is automatically inserted into their Player object. When the player's Character spawns for the first time all of the contents of StarterGui are automatically copied into the player's PlayerGui. Note that if CharacterAutoLoads is set to false the character will not spawn and StarterGui contents will not be copied until LoadCharacter is called. If ResetPlayerGuiOnSpawn is set to true then every time the player's character respawns all of the contents of that player's PlayerGui is cleared and replaced with the contents of StarterGui.
Basically, if you try to edit something from the StarterGui
, it won’t be visible until the player is reset. You have to edit it from PlayerGui
, so in your code, instead of game:GetService("StarterGui")
do:
player = game:GetService("Players").LocalPlayer playerGui = player:WaitForChild"PlayerGui" aboutButton = playerGui.ScreenGui.MainMenuFrame.MainMenuAboutButton aboutButton.x = 0.5