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:
1 | script.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 |
8 | end ) |
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:
1 | function onClicked(playerWhoClicked) |
2 | script.Parent.BrickColor = BrickColor.Random() |
3 | end |
4 |
5 | script.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.
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:
01 | local part = script.Parent --The part that has the sound in it |
02 | local db = false --Debounce boolean. This is too prevent repetition of the script. |
03 |
04 | function playmusic(hit) --the function start |
05 | local human = hit.Parent --The person who clicked the sound |
06 | if not db then |
07 | if human then -- if the player clicked it, then continue with script |
08 |
09 | db = true --sets db to true |
10 |
11 | part.Sound:Play() --plays the sound |
12 | wait( 10 ) -- waits for 10 seconds |
13 | part.Sound:Stop() -- stops sound |
14 |
15 | db = false --changes db to false |
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.
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,
1 | function onClicked(playerWhoClicked) |
2 | script.Parent.BrickColor = BrickColor.Random() |
3 |
4 | script.Parent.Sound:Play() -- Copied from first script |
5 | end |
6 |
7 | script.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.
1 | function onClicked(playerWhoClicked) |
2 | script.Parent.BrickColor = BrickColor.Random() |
3 |
4 | local sound = script.Parent.Sound:Clone() |
5 | sound.Parent = playerWhoClicked |
6 | sound:Play() |
7 | end |
8 |
9 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
If you want the sound to stop after ten seconds, just wait(10)
and stop the sound with :Stop()
.
01 | function 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 |
11 | end |
12 |
13 | script.Parent.ClickDetector.MouseClick:Connect(onClicked) |
Let me know if you have any questions down below, and I should be able to help.
Good Luck!
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!
This works in my mind. And add a ClickDetector in the part. Don't add anything else like the sound.
01 | local Debounce = false --stops it from running a million times at once |
02 | function onClicked(plr) --I changed it to plr because it is shorter, and is what most people use. |
03 | if Debounce = = false then --checks if it is not running a million times at once |
04 |
05 | local Debounce = true --makes it think it is running a millions times at once |
06 |
07 | Instance.new( "Sound" , game.Workspace. [ plr.Name ] .Head) |
08 | game.Workspace. [ plr.Name ] .Head.Sound.SoundId = 'rbxassetid://123456' -- Replace 123456 with what your audio ID is. |
09 | game.Workspace. [ plr.Name ] .Head.Sound.Playing = true |
10 |
11 | wait( 10 ) |
12 |
13 | game.Workspace. [ plr.Name ] .Head.Sound.Playing = false |
14 |
15 | local Debounce = false --makes it think it is not running a million times at once |
Look at the script and learn a bit!