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

How can I use a variable to change properties of an ImageButton?

Asked by 6 years ago
Edited 6 years ago

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!

2 answers

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

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
0
Thank you! coolguye12 5 — 6y
0
I got it working! I wish I could upvote you but the site won't let me. coolguye12 5 — 6y
0
You can mark one of the answers as accepted though Amiaa16 3227 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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
0
Thank you! coolguye12 5 — 6y
0
I got it working! I wish I could upvote you but the site won't let me. coolguye12 5 — 6y
0
Yeah but you can accept the answer ^ :) User#19524 175 — 6y
0
Hate to ask another question...but how do I do that? lol :) coolguye12 5 — 6y
View all comments (2 more)
0
Click on “Accept Answer” under this one. :) User#19524 175 — 6y
0
Weird. I can't see accept answer anywhere, only 'Report'. coolguye12 5 — 6y

Answer this question