#bug music player toggle on and off dosent work after resetting

1 messages · Page 1 of 1 (latest)

unkempt crater
#
local LocalPlayer = game:GetService("Players").LocalPlayer
local gui = LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MusicGui")
local MusicLabel = gui.MusicName

local ToggleButton = gui.ToggleButton
local PlayImage = "rbxassetid://2876994160"
local PauseImage = "rbxassetid://6417985201"

local MusicFolder = game:GetService("Workspace"):FindFirstChild("MusicFolder")
local debounce = false

local OldMusic
-- // 

ToggleButton.Activated:Connect(function()
    if debounce == false then
        debounce = true
        if ToggleButton.Image == PauseImage then -- We Need to Mute Music
            ToggleButton.Image = PlayImage
            if OldMusic ~= nil then
                local MusicInstance = MusicFolder:FindFirstChild(OldMusic)
                if MusicInstance ~= nil then
                    MusicInstance.Volume = 0
                end
            end
        else -- We Need to Unmute Music
            ToggleButton.Image = PauseImage
            if OldMusic ~= nil then
                local MusicInstance = MusicFolder:FindFirstChild(OldMusic)
                if MusicInstance ~= nil then
                    MusicInstance.Volume = MusicInstance:FindFirstChild("Volume").Value
                end
            end
        end
        wait(0.15)
        debounce = false
    end
end)
#
for _, sound in pairs(MusicFolder:GetChildren()) do
    if sound:IsA('Sound') then
        local NewString = Instance.new("StringValue")
        NewString.Value = sound.Volume
        NewString.Name = "Volume"
        NewString.Parent = sound
        
        sound.Volume = 0
    end
end

function ReturnRandomSong()
    local Children = MusicFolder:GetChildren()
    local Index = math.random(#Children)
    local Int = Children[Index]
    
    local FinalInstance = MusicFolder:FindFirstChild(Int.Name)
    
    if OldMusic == FinalInstance.Name then
        return ReturnRandomSong()
    else
        return FinalInstance
    end
end

while true do
    
    if OldMusic ~= nil then
        local MusicInstance = MusicFolder:FindFirstChild(OldMusic)
        if MusicInstance ~= nil then
            MusicInstance.Volume = 0
        end
    end
     
    local Sound = ReturnRandomSong()
    OldMusic = Sound.Name
    MusicLabel.Text = OldMusic
    
    if ToggleButton.Image == PauseImage then
        Sound.Volume = Sound:FindFirstChild("Volume").Value    
    end
    
    Sound:Play()
    
    wait(Sound.TimeLength)
end
timber storm
#

as a developer of a mp3 player raspberry pi 4 model b with touchscreen I can say that usually the error lies between OldMusic beeing defined as nothing. I would at least define it as nil and also use type to make it more clean and easy to read