Mute Button UI Not Working

Here is my code,

MuteMusic = game.StarterGui.ScreenGui.MuteMusic
function mute()
	MuteMusic.Image = 176572847
	if MuteMusic.Image == 176572847 then
		MuteMusic.Image = 176572748
	end
end

script.Parent.MouseButton1Click:Connect(mute)

MuteMusic is the ImageButton and I want the image to change from 176572847 to 176572748 or the other way round if you click it (i dont need hlep with the music i just need help with the ui changing)
can someone explain why my code doesn’t work?

2 Likes

MuteMusic.image doesnt take a Number Value, You should syntax it like this:

MuteMusic = game.StarterGui.ScreenGui.MuteMusic
function mute()
	MuteMusic.Image = "rbxassetid://176572847"
	if MuteMusic.Image == "rbxassetid://176572847" then
		MuteMusic.Image = "rbxassetid://176572748 "
	end
end

script.Parent.MouseButton1Click:Connect(mute)

Edit: Also I think the code is not optimal since everytime mute() is called it will always set the image to the second ID. Beacuse inside the function you set the ID to the first one so the the If statement is unecessary.

1 Like

You can use the classic x = not x to toggle the value, instead of manually setting it to true or false.
In this case, I would make a true/false variable, then that can then set the image.

Like this:

local muteMusic = game:GetService("StarterGui").ScreenGui.MuteMusic
local muted = false

MuteMusic.Image = "rbxassetid://0" --unmute

script.Parent.MouseButton1Click:Connect(function()
    muted = not muted --Toggles the value.
    if muted == false then
        MuteMusic.Image = "rbxassetid://0" --unmute
    else
        MuteMusic.Image = "rbxassetid://0" --mute
    end
end)
2 Likes