Why is this script not working?

local imageButton = script.Parent
local sound = game.Workspace:WaitForChild("Sound")

local image1 = "rbxassetid://13675504613" -- sound on image
local image2 = "rbxassetid://14234283427" -- sound off image

local isMuted = false

local function toggleSound()
	if isMuted then
		sound.Volume = 1
		imageButton.Image = image1
	else
		sound.Volume = 0
		imageButton.Image = image2
	end
	isMuted = not isMuted
end

imageButton.MouseButton1Click:Connect(toggleSound)

Hello,
I’m currently trying to make a volume on/off button but when I click on it, the button completely disappears and doesn’t change decal, but them music stops. When I click on it again (in theory it should make the music’s volume go back to 1 again) it doesn’t work and the decal doesn’t change back to default.
Is there something I’m doing wrong?

Thanks in advance

This should work:

local imageButton = script.Parent
local sound = game.Workspace:WaitForChild("Sound")

local isMuted = false

local function toggleSound()
	if isMuted == true then
		sound.Volume = 1
		imageButton.Image = 13675504613
		isMuted = false -- you must set isMuted false here, after it got clicked while being true
	else
		sound.Volume = 0
		imageButton.Image = 14234283427
		isMuted = true -- you must set isMuted true here, after it got clicked while being false
	end
-- dont make isMuted false here, it won't work like this because then it is always false.
end

imageButton.MouseButton1Click:Connect(toggleSound)

The image still disappears when I click it and the decal doesn’t change

Are there any errors in the output?

local imageButton = script.Parent
local sound = game.Workspace:WaitForChild("Sound")

local isMuted = false

local image1 = "http://www.roblox.com/asset/?id=13675504556" -- sound on image
local image2 = "http://www.roblox.com/asset/?id=14234283407" -- sound off image

local function toggleSound()
	if isMuted == true then
		sound.Volume = 1
		imageButton.Image = image1
		isMuted = false
	else
		sound.Volume = 0
		imageButton.Image = image2
		isMuted = true
	end
end

imageButton.MouseButton1Click:Connect(toggleSound)

now it should be working. I deleted it a few times because I pasted in a wrong image id

I changed it again. Sorry for the confusion but id’s in scripts are sometimes not working for me.

This works! Thanks a lot for the help and the fast reply

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.