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?
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.
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.
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
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.
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 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