Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Click to play music?

Asked by 8 years ago

Hello!

I can't get my script to work, so I need help. What I really want is for people to click on an item and the music starts playing for like 10 seconds. After that it will automatically stop until clicked again. It will be even more ideal if only the person who clicks the item will hear the music!

So far, I have two scripts and I don't know how to combine the two for it to work. So please, please someone smart out there help me with my script!

What I do now (for example) is create a brick, insert "sound" (that I looked up in my toolbox) add a script into my brick with the following code:

1script.Parent.Touched:connect(function(part)
2    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
3 
4    if plr then
5        script.Parent.Sound:Play()
6    end
7 
8end)

This will work if you walk over the brick, but it is not what I want. So I tried to insert a ClickDetector (right click on my brick and add the ClickDetector) Add a script with the following code:

1function onClicked(playerWhoClicked)
2    script.Parent.BrickColor = BrickColor.Random()
3end
4 
5script.Parent.ClickDetector.MouseClick:connect(onClicked)

However this will not work. I think the problem is they need to be combined, but I never done scripting so I don't know how to do it. The sound script (the first one) also doesn't stop the music after 10 seconds. And of course there is still the issue with the one who clicks the sign/brick only hears the audio.

I know this is probably not easy, but I'm just trying my luck here on this forum.

If you have any suggestions, please write them down. It is very much appreciated.

4 answers

Log in to vote
0
Answered by
Peeshavee 226 Moderation Voter
8 years ago
Edited 8 years ago

Ok, so I'm still new to scripting, and I'm not a pro. I'm also not to good with describing things. But I'll make a script that I'm pretty sure will work, using my own ways. Here it is:

01local part = script.Parent   --The part that has the sound in it
02local db = false  --Debounce boolean. This is too prevent repetition of the script.
03 
04function playmusic(hit) --the function start
05local human = hit.Parent  --The person who clicked the sound
06if not db then
07if human then  -- if the player clicked it, then continue with script
08 
09db = true --sets db to true
10 
11part.Sound:Play()  --plays the sound
12wait(10-- waits for 10 seconds
13part.Sound:Stop() -- stops sound
14 
15db = false --changes db to false
View all 22 lines...

You need to add a ClickDetector in the part, and add a sound id into the Sound part. Hope I helped! If I did please accept my answer.

0
The MouseClick function returns the player, not what.... "Hit"? User#11440 120 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

So long as the sound is in the part along with the script, the script I'm going to show you will work.

We can just copy the code from the top script down to the second like this,

1function onClicked(playerWhoClicked)
2    script.Parent.BrickColor = BrickColor.Random()
3 
4    script.Parent.Sound:Play()-- Copied from first script
5end
6 
7script.Parent.ClickDetector.MouseClick:connect(onClicked)

Now, if you want the sound to play for the player who clicked the brick, and them only, copy the sound into the player who clicked, and play it.

1function onClicked(playerWhoClicked)
2    script.Parent.BrickColor = BrickColor.Random()
3 
4    local sound = script.Parent.Sound:Clone()
5    sound.Parent = playerWhoClicked
6    sound:Play()
7end
8 
9script.Parent.ClickDetector.MouseClick:connect(onClicked)

If you want the sound to stop after ten seconds, just wait(10) and stop the sound with :Stop().

01function onClicked(playerWhoClicked)
02    script.Parent.BrickColor = BrickColor.Random()
03 
04    local sound = script.Parent.Sound:Clone()
05    sound.Parent = playerWhoClicked
06    sound:Play()
07    wait(10)
08    sound:Stop()
09 
10    sound:Destroy()-- Cleanup, technically optional
11end
12 
13script.Parent.ClickDetector.MouseClick:Connect(onClicked)

Let me know if you have any questions down below, and I should be able to help.

Good Luck!

If I helped, please don't forget to accept my answer.
0
But this is also only locally right? Missy_Idiot 0 — 8y
0
Should be, yes. User#11440 120 — 8y
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Wow! I did exactly as you said Peeshavee, and it worked! I swear I could kiss you right now.

And this music can now only be heard to the one who's clicking on the brick right?

Thank you so so so so much for your effort. I am really thankful!

Edit:

wfvj014 I just saw your reply as well, thank you so much for explaining this to me. I think Peeshavee combined everything you said in one script right?

Thnx again guys, really!

0
Who? User#11440 120 — 8y
0
Also, accept the person's answer who helped. User#11440 120 — 8y
0
His answer does contain false information though, and would not play locally. User#11440 120 — 8y
0
You can post comments on others' answers. And no, his does not. In his script, you could remove lines 5,7, and 17 without consequence. Mine actually works :P. Also, accept an answer please. User#11440 120 — 8y
Log in to vote
0
Answered by 8 years ago

This works in my mind. And add a ClickDetector in the part. Don't add anything else like the sound.

01local Debounce = false --stops it from running a million times at once
02function onClicked(plr) --I changed it to plr because it is shorter, and is what most people use.
03if Debounce == false then --checks if it is not running a million times at once
04 
05local Debounce = true --makes it think it is running a  millions times at once
06 
07Instance.new("Sound", game.Workspace.[plr.Name].Head)
08game.Workspace.[plr.Name].Head.Sound.SoundId = 'rbxassetid://123456' -- Replace 123456 with what your audio ID is.
09game.Workspace.[plr.Name].Head.Sound.Playing = true
10 
11wait(10)
12 
13game.Workspace.[plr.Name].Head.Sound.Playing = false
14 
15local Debounce = false --makes it think it is not running a million times at once
View all 25 lines...

Look at the script and learn a bit!

0
What i meanh by running a million times at once is running mutiple times at once. SH_Helper 61 — 8y
0
SH this script did not work :O Missy_Idiot 0 — 8y
0
this is not gonna work by itself, use the instructions in green. SH_Helper 61 — 8y

Answer this question