Reputation: 17
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
while true do
wait()
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
d:Play()
wait(d.TimeLength)
mutebutton.MouseButton1Click:Connect(function()
d.Volume = 0
end)
end
If i was to replace d.Volume = 0 with print(d.Volume) or print("testing") then it will work however when I change it to actually mute the audio, it dosent want to work. Anyone know why?
Upvotes: 1
Views: 53
Reputation: 169
The main problem with your script is that the MouseButton1Click:Connect
(listening for clicks) is done only after the wait(d.TimeLength)
(after the song has finished playing).
Adding on from ivy's suggestion of using d:Pause()
, I would suggest doing something like this:
while true do
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local d = b[math.random(1, #b)]
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then -- Makes the pause button a play button if the song is paused already
d:Resume()
else
d:Pause()
end
end)
d.Ended:Wait() -- Wait until music ends
connection:Disconnect() -- Remove connection (to prevent multiple functions on the same button click)
end
Upvotes: 1