New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

How do you turn off music by script???

Asked by 8 days ago

Sooo I am making a game where you press "p" it starts playing the box "Music" so how do I check if the music is on and if it is press "p" again to turn it off

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.P then -- The Music player
end
end)

2 answers

Log in to vote
0
Answered by 8 days ago

I'm not sure if this will work, I don't have studio on so this is pure memory...

local Sound = script.Parent.Sound -- Change this to where the sound is located
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.P then -- The Music player
        if Sound.IsPlaying then -- If sound is playing
            Sound:Pause() -- Pauses the sound
        elseif not Sound.IsPlaying then -- if the sound is paused
            Sound:Resume() -- Continues playing it
        end
    end
end)

Hope this helps! :D

0
Don't forget about gameProcessedEvent on InputBegan FloweryMonkeyboy5 984 — 8d
0
Your script worked the best !! Smartics 25 — 8d
Ad
Log in to vote
0
Answered by 8 days ago
Edited 8 days ago
local UserInputService = game:GetService("UserInputService")
local YourSound = game:GetService("SoundService").yoursound.IsPlaying

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.P then -- The Music player
        YourSound = false
    else
        YourSound = true
    end
end)

This script will set the IsPlaying value to false, successfully stopping the sound/music you are playing.

(p.s replace the "yoursound" after "game:GetService("SoundService")" with the name of your sound, also make sure it's under SoundService!

Accept if this helped!

0
Don't forget about gameProcessedEvent on InputBegan FloweryMonkeyboy5 984 — 8d

Answer this question