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

[SOLVED]How do I get a sound to play w/ a click detector?

I've been looking around the wiki for scripts on onClicked scripts and tried configuring

function onClicked(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()
end
 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

to work with playing sounds (of course deleting the brick color changing bit) but to no avail. I'm trying to use "Play ( )" but all the time it says "Unknown global 'Play'"

For example, this script

function onClicked(playerWhoClicked)
	Play ( )
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

shows the Unknown global "Play" message.

I also tried

function onClicked(playerWhoClicked) 
	do Play ( )
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Which didn't give me the error, but when I tried it in game it didn't work.

Finally! After messing around with the things, I figured out what I needed to do. With the help from both of the answers, I came up with this! Sadly I can't give rep to both, but I mainly got the help from the first.

local sound = game.workspace.Sound

function onClicked(playerWhoClicked)
	sound:Play ( )
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

For anyone looking at this as help, you can rename "Sound" in the first line to anything your sound is renamed to.

2 comments

0

Mind posting the script that gave you this error? Once we have that, we should be able to help!

0

^ I posted another piece of script that gave me the error.

2 answers

✓ Accepted answer

Here's what I would do:

local part = script.Parent --the part that we will click
local sound = game.Workspace.Sound --the sound that will play in workspace. Change this to wherever your song will be located.
local db = false

function click(hit)
local human = hit.Parent
if human then
if not db then

db = true

sound:Play()
wait(60) -- change this to how long the sound plays for. i the sound is 2 seconds longs, put in 2

db = false

end
end
end

part.ClickDetector.MouseClick:connect(click)

I hope this helped, and if it did, don't forget to accept my answer! If you have questions, feel free to comment!

4 comments

0

There are a few errors in your code - specifically, no ends.

0

Gosh, I completely forgot. I was just typing it out here. So sorry about that. I'm still new ._.

0

You may just want to review this code and add in the 'end's that need to be put in!

0

I did :D

The problem is that Play() is a method, not a function.

Methods get called on an object, obj:MethodName(). Functions can just get called whenever, FunctionName().

As well as this, Play() can only be accessed on Sound objects.

function clicked()
	script.Sound:Play() -- if there was a Sound inside the script
end

script.Parent.ClickDetector.MouseClick:connect(clicked)

Hope I helped!

~TDP

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