Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you turn off music by script???

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

✓ Accepted answer

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

2 comments

0

Don't forget about gameProcessedEvent on InputBegan

0

Your script worked the best !!

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!

1 comment

0

Don't forget about gameProcessedEvent on InputBegan

This discussion is preserved as it was. New answers, comments, and votes are closed.