Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does the image not change when I click the button?

I am a beginner scripter and I am trying to make a shop GUI. I have added an ImageButton with the following script:

local button = script.Parent
local shopgui = game.StarterGui.ShopGUI.Frame

button.MouseButton1Click:Connect(function()
	if button.Image == 2596564913 then
		shopgui.Visible = false
	end
	shopgui.Visible = true
	button.Image = 2596564913
end)

I am wondering why it doesn't work. It is supposed to change the image to an X symbol. The if statement is supposed to check if the image is the X, then to close the Shop GUI.

However, when I playtested, I clicked on the shop icon and it just turned the image blank, and didn't even open the GUI. I've tried adding "else" to it, but it doesn't work either.

All help is appreciated :)

5 comments

0

Try to use print() statements. They help alot. Then tell me the results.

0

So, I added print("Shop Closed") inside the if statement and print("Shop Opened") under the if statement. I ran the game, clicked the button but still got no result.

0

I think you gotta do "rbxassetid://2596564913" make sure it's a string in your script

0

again, you're trying to change the startergui, which obviously won't work. change the playergui instead. @TheBravestRobloxian

0

also, like what @MarkedTomato said, you need to get the rbxassetid of an image id

1 answer

✓ Accepted answer

##Problem:

It's because you're getting the StarterGui itself. StarterGui is a container for all GUI'S in ROBLOX. (When a player joins the game all the GUI's and all contents are automatically cloned in StarterGui)

##SOLUTION:

Use PlayerGui when referencing StarterGui

Fixed Scripts:

local player = game.Players.LocalPlayer
PlayerGui = player:WaitForChild("PlayerGui").ShopGUI.Frame
local button = script.Parent-- change to where your script is located

button.MouseButton1Click:Connect(function()
    if button.Image == "rbxassetid://2596564913" then
    PlayerGui.Visible = false
    PlayerGui.Visible = true
        button.Image = "rbxassetid://2596564913"
        end
end)

OR: (IF you wanted the script to work in StarterPlayerScripts.)

Put the script in StarterPlayer < StarterPlayerScripts.

local player = game.Players.LocalPlayer
PlayerGui = player:WaitForChild("PlayerGui").ShopGUI.Frame
local button = player.PlayerGui.Button -- change to where your script is located

button.MouseButton1Click:Connect(function()
    if button.Image == "rbxassetid://2596564913" then
    PlayerGui.Visible = false
    PlayerGui.Visible = true
        button.Image = "rbxassetid://2596564913"
        end
end)

##Recommendation(s):

  • Like what @MarkedTomato said, use "rbxassetid://2596564913". This actually gets the id of an image.

Let me know if you have more questions.

#JesseSong

This discussion is preserved as it was. New answers, comments, and votes are closed.