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

I made a script and tested it, and it didn't work, what did I do wrong?

I was making a script with a SurfaceGUI button that would play/stop music if it was clicked on. It tried the below script but it didn't work.

local button = script.Parent
local sound = button:WaitForChild('Sound')

local function onButtonClick()
	if sound.IsPlaying then
		sound:Stop()
		else sound:Play()
	end
end

button.MouseButton1Click:connect(onButtonClick)

What did I do wrong?

2 comments

0

First off, are you using a local script?

0

Yes, I am using a local script, but the button is visible to the entire server (it's on a brick).

1 answer

So you were accessing the button not the sound when you did your sound variable.

--Make sure the sound is inside the frame.
local button = script.Parent
local sound = script.Parent.Parent.Sound --Put the name of the sound object here

local function onButtonClick()
    if sound.IsPlaying then
        sound:Stop()
    else 
	    sound:Play()
    end
end

button.MouseButton1Click:connect(onButtonClick)

10 comments

0

I copy/pasted the script above and the sound didn't play when I clicked the button.

0

A regular script works just not a LocalScript.

0

it still isn't working

0

Where is your sound? Also, open your output to check for errors and are you using a normal script?

0
  1. My sound is located in the item "SurfaceGUI" 2. I'm using a normal script 3. Output is blank. I click on the button and nothing happens, but no error.
0

Make the Sound in the same frame as the Button.

0

Alright, my script is NOT local, and the sound is located in the same object as the button. Yet, nothing is happening. Do you think it could be an error with the sound itself, or with the GUI? I've tried everything and no music is playing at all.

0

Do you have an ID in the sound?

0

yes

0

Okay so the button is in the same frame as the sound right? If it isnt then it would need to be. And is the sound named sound?

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