Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

mute/unmute text on button not working, can i get some help?

Asked by 4 months ago

I'm trying to make a mute music button if where you click the button, the text changes to "Unmute Music" and changes back to "Mute Music" when you click it again.

Here is the code I used and it's not working:

local button = script.Parent --this is the mute button

button.MouseButton1Click:Connect(function()
    if button.Text == "Mute Music" then
        button.text = "Unmute Music"
    end
    if not button.Text == "Mute Music" then
        button.Text = "Mute Music"
    end
end)

Can someone tell me what I did wrong?

2 answers

Log in to vote
3
Answered by
kingdom5 5336 Trusted Snack Break Moderation Voter Community Moderator
4 months ago
Edited 4 months ago

The problem is that your first if statment will cause the second if statment to also run meaing to you the text does not change.

local button = script.Parent --this is the mute button

button.MouseButton1Click:Connect(function()
    if button.Text == "Mute Music" then
        button.Text = "Unmute Music" -- this will cause the second if statment to be true and run
    end

    if not button.Text == "Mute Music" then
        button.Text = "Mute Music"
    end
end)

You can solve this by useing else which will mean if other then or use elseif which means that if one statment is true the others will not run within the block.

local button = script.Parent --this is the mute button

button.MouseButton1Click:Connect(function()
    if button.Text == "Mute Music" then
        button.Text = "Unmute Music"
else -- as you are doing a normal comparison this is best as you know it is not true in all cases
        button.Text = "Mute Music"
    end
end)

hope this helps.

0
ok thanks the script works, i also forgot to capatilize the word "text" on the 5th line of the script so that also caused the script to not work. 1swenzje 27 — 4mo
0
Yep I did not see that as well. kingdom5 5336 — 4mo
Ad
Log in to vote
0
Answered by
gioni01 2055 Moderation Voter Community Moderator
4 months ago

It's because of your usage of not. Really, all you need to do is use an else statement.

local button = script.Parent --this is the mute button

button.MouseButton1Click:Connect(function()
    if button.Text == "Mute Music" then
        button.text = "Unmute Music"
    else
        button.Text = "Mute Music"
    end
end)

Answer this question