Can't change my BoolValue from a script

I’m trying to make a simple ImageButton to mute/unmute some sound effects in my game, and I can’t seem to get it to work. I tried a LocalScript and a normal Script, but nothing seems to change the value or the image. I tested it with print statements, and it went through with those, but it doesn’t change the actual properties.

local image = script.Parent.Image
image = "rbxassetid://8083073043"

local isMuted = script.Parent.Muted.Value

script.Parent.MouseButton1Click:Connect(function()
	if isMuted == false then
		print('true')
		isMuted = true
		image = "rbxassetid://8083072917"
	else
		print('false')
		isMuted = false
		image = "rbxassetid://8083073043"
	end
end)

The ImageButton is parented to a ScreenGui in StarterGui

3 Likes

You can use the MouseButton1Click only on a LocalScript, you can change it on the Server with a Event.

1 Like

From my understanding, the property isn’t being changed because the “isMuted” variable has been assigned a copy of the value that was returned rather than holding a direct reference to the property of the object. When you set isMuted = true, it’s updating the value that’s assigned to the variable to be true rather than updating the Value property of the intended object.


Take this explanation of mine with a grain of salt since I’m not very familiar with the technical terminology in regards to how that works. I had read a very thorough/clear explanation of this on the Developer Forum before but I can’t quite find it. I’ll update this post if I come across it/if someone else provides a more accurate response.


Example

local isMuted = script.Parent.Muted -- Refers to the Instance called "Muted"

script.Parent.MouseButton1Click:Connect(function()
	if isMuted.Value == false then -- If the Value property of the "Muted" Instance is false, then...
       
        isMuted.Value = true -- Sets the Value property of the referenced Instance to true

        -- Continue
    end
end)
5 Likes
local image = script.Parent
local isMuted = script.Parent.Muted

script.Parent.MouseButton1Click:Connect(function()
	if isMuted.Value == false then
		print('true')
		isMuted.Value = true
		image.Image = "http://www.roblox.com/asset/?id=8083072917"
	else
		print('false')
		isMuted.Value = false
		image.Image = "http://www.roblox.com/asset/?id=8083073043"
	end
end)

It looks like you are getting the value of the instance, and not the instance itself. If you did local image = script.Parent.Image, or local isMuted = script.Parent.Muted.Value , you would be getting the values of them. This means you wouldn’t be able to set them, and the variable would represent the value of the property when you got it. I changed rbxassetid:// because it has caused me issues in the past, and http://www.roblox.com/asset/?id= has solved them.

4 Likes

That’s because you’re attempting to change the value of a BoolValue instance from the client (through a local script) meaning that the change won’t replicate to the server (won’t be observed by the server) and only the client itself will experience the change.

In addition to the above, you’re also just assigning the value stored by the BoolValue instance to the variable named “isMuted” which means that the variable isn’t a reference to the BoolValue instance itself, thus when you next assign a value to “isMuted” the previous value is overridden but you’re not assigning the value to the value of the BoolValue instance itself you’re just assigning it to the variable.

And one more thing, you’re attempting to assign an image to the “ImageLabel”/“ImageButton” instance itself not the Image property of the instance which is where you should be assigning the image to.

local imageLabel = script.Parent
imageLabel.Image = "rbxassetid://8083073043"
local isMuted = script.Parent:WaitForChild("Muted")

script.Parent.MouseButton1Click:Connect(function()
	if not isMuted.Value then
		print('true')
		isMuted = true
		imageLabel.Image = "rbxassetid://8083072917"
	elseif isMuted.Value then
		print('false')
		isMuted = false
		imageLabel.Image = "rbxassetid://8083073043"
	end
end)
2 Likes
isMuted.Value = false -- add .Value at the end
1 Like

Just reminding you to edit “fasle” to “false”.

Try changing this value with a remote event, example:

local event = workspace.ChangeValue
event:FireServer(true) --Changing value to true

in the event script:

local event = script.Parent

event.OnServerEvent:Connect(function(plr, value)
	--
	plr.PlayerGui.ScreenGui.TextButton.BoolValue.Value = value
	--
end)

1 Like