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

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

Asked by 3 years ago
Edited 3 years ago

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 :)

0
Try to use print() statements. They help alot. Then tell me the results. Soban06 410 — 3y
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. TheBravestRobloxian 2 — 3y
0
I think you gotta do "rbxassetid://2596564913" make sure it's a string in your script MarkedTomato 810 — 3y
0
again, you're trying to change the startergui, which obviously won't work. change the playergui instead. @TheBravestRobloxian JesseSong 3916 — 3y
0
also, like what @MarkedTomato said, you need to get the rbxassetid of an image id JesseSong 3916 — 3y

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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

Ad

Answer this question