Music isn't working

function on()
isOn = true
script.Parent.Winner:Pause()
end

function off()
isOn = false
script.Parent.Winner:Play()
end

function onClicked()

if isOn == true then
	off() 
else 
	on()
end

end

script.Parent.MouseButton1Click:Connect(onClicked)

on()


So basically i want it so when i press a text button the music plays and if clicked again it’ll pause.
I want to learn how to make to improve on this script by learning why it isn’t putting the music on or playing it after it was resumed?

For code that doesn’t work or needs debugging, please have it posted in #help-and-feedback:scripting-support

Code Review is for working code that needs polishing/improvements

Also please read this post on posting code on the forum Inserting Code Snippets

I have changed it, Thank you though

Might need some more context. I assume this script is parented inside a button? If so, did you make sure it was a LocalScript? Also, have you tried playing the sound before at all to make sure it works?

Also, it is good practice to define your variables that you use. While it won’t really change the behavior of your script, I would recommend putting local isOn = false at the top of the script.

1 Like

Yea, the script is inside of the button and so is the sound and the sound works without a script, but i want it to work when i click the button.

I also want the music to play in game and not just for one player so that’s why i am not using a local script.

To help you with your code:

local isOn = false
local function onClicked()
    isOn = not isOn
    if isOn then
        script.Parent.Winner:Play()
    else
        script.Parent.Winner:Pause()
    end
end

script.Parent.MouseButton1Click:Connect(onClicked)

I think the issue with your code is the lack of the isOn being available through out the script? There was not enough information provided for a sure answer. But the code I provided should fix the issue.

Basically, when the button is clicked, it will make isOn the opposite value, so if it’s set to true, it will become false (this is what not is for)

then the if statement will check whether it has been set to true or false and will play or pause depending on that.

2 Likes

That’s your problem. Scripts don’t work inside the GUI because they only exist on the client. You’ve gotta use a LocalScript.

If you want to then play the sound for all players, you have a couple options:

  1. Use a RemoteEvent and a normal Script in ServerScriptService to play/pause the sound.
    OR
  2. Put the sound in the workspace and make sure SoundService.RespectFilteringEnabled is off.
1 Like

Thank you for that explanation, I am going to add that in and see if it works. I don’t usually use NOT, but i can see how it works and is necessary. Thanks

I tried the script you give me and it didn’t work?

Because it’s a normal script. As what @Crazyman32 has mentioned, normal scripts do not run when placed under playergui

1 Like

I didn’t even know that, god i have a lot of learning lmao. I will try it now thanks

Thank you so much, i’ve been going crazy over such a simple thing. Thank you for helping me both of you and also helping me learn and improve :slight_smile:

1 Like

Oops… It works however no one else can hear it? Is there any possible way to make them hear it too? Since i know local scripts are client based.

Remote Events allow you to communicate between the server and clients. Basically, you need a RemoteEvent under ReplicatedStorage and fire this remote whenever the button is clicked:

--The local script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

local isOn = false
local function onClicked()
    isOn = not isOn
    if isOn then
        Remote:FireServer()
        script.Parent.Winner:Play()
    else
        Remote:FireServer()
        script.Parent.Winner:Pause()
    end
end

local function OnClientEvent(Data)
    isOn = Data
    if isOn then
        script.Parent.Winner:Play()
    else
        script.Parent.Winner:Pause()
    end
end

script.Parent.MouseButton1Click:Connect(onClicked)
Remote.OnClientEvent:Connect(OnClientEvent)
--Normal script in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")

local function OnServerEvent(Player, Data)
    for i, v in ipairs(Players:GetPlayers()) do
        if v ~= Player then
            Remote:FireClient(v, Data)
        end
    end
end

Remote.OnServerEvent:Connect(OnServerEvent)

This is just the basic foundation of a client to server communication model. You will have to expand this further to prevent exploits and much more.

The code I posted can be further improved, but I don’t really feel so well right now. I hope you can take the time to try the code and explore it.

Thank you! I will read through this code and improve and learn from it.

1 Like

Okay, So I’ve added the script you created and i’ve read through it and i get what it’s doing it’s basically telling the event to fire the function to the players client when clicked. However the music is still only working on one? I am going to re read it and try to do it myself and see if i can fix it and if i can’t i’ll return here :slight_smile: Thanks for all help btw

I forgot to add something in the if statements here:

if isOn then
    Remote:FireServer(true) -- add true here
    script.Parent.Winner:Play()
else
    Remote:FireServer(false) --add false here
    script.Parent.Winner:Pause()
end

It works! Omg you’re such a life saver.

You can learn more about Remote Events on the developer hub:

You can also learn other things related to scripting here, such as when to use RemoteFunctions over RemoteEvents

If you have any questions about any part of the code I provided, please don’t hesitate to ask.

1 Like