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

uhm i want to know how to make a block that plays music without repeating?

i want to know how to make a block that plays music without repeating when you click it and when clicking again the music stops too

this is the script i did

local touch = workspace.music.ClickDetector

local music = Instance.new("Sound", workspace.music)

music.SoundId = "rbxassetid://890276873"

function onMouseClick ()
	
	music:Play()
	
end

touch.MouseClick:connect(onMouseClick)

3 answers

Alright so I redid the entire script into my own version to make sure I could get everything to work.

What you did wrong...

So you have it where when you click the brick, it turns the audio on and nothing to counter it if it's pressed again, it just keeps looping that audio and over-riding it or duplicating it. On-top of that, it will get very tedious to get the audio IDs all the time, i'd recommend just adding a folder named MusicFolder to Workspace and putting your sounds there and naming them what you want them to be named.

Look into these things.

I would recommend looking into if statements Here's a video from AlvinBlox explaining if statements.

What you should improve on...

  1. Understanding if statements and audios.
  2. Learn about GUIs as they can be more easier to control.
  3. Look more into print() statements as they can help you determine what's not working.

What I did.

I rewrote the entire script so that way you could do the following...

  1. Have a folder for all your sounds instead of using IDs as that can become a hassle to do consistently.
  2. Made it so everything was organized
  3. Made it so it turned itself off and reset itself. To change its reseting of itself, remove the This resets the audio. line.

Script I made.

--Made by MillerrIAm
----------------Variables-------------
Brick = game.Workspace.MusicBrick --The brick you're going to click.
Music = game.Workspace.MusicFolder --A folder I made to hold the sounds.
ClickDetector = Brick.ClickDetector --This connects to the click detector
---------------Main Script--------------
function PlayMusic()
	if Music.MULE.Playing == false then 
		Music.MULE.Playing = true --This turns the audio on.
		print(1) --This informs you that it's working correctly.
	else
		Music.MULE.Playing = false --This turns the audio off.
		Music.MULE.TimePosition = 0 --This resets the audio.
		print(2)--This informs you that it's working correctly.
	end
end

ClickDetector.MouseClick:Connect(PlayMusic)
print("Entire Script Working")

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