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 7 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:

script.Parent.Touched:connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr then
        script.Parent.Sound:Play()
    end

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:

function onClicked(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()
end

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.

4 answers

Log in to vote
0
Answered by
Peeshavee 226 Moderation Voter
7 years ago
Edited 7 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:

local part = script.Parent   --The part that has the sound in it
local db = false  --Debounce boolean. This is too prevent repetition of the script.

function playmusic(hit) --the function start
local human = hit.Parent  --The person who clicked the sound
if not db then
if human then  -- if the player clicked it, then continue with script

db = true --sets db to true

part.Sound:Play()  --plays the sound
wait(10)  -- waits for 10 seconds
part.Sound:Stop() -- stops sound

db = false --changes db to false

end
end
end 


part.ClickDetector.MouseClick:connect(playmusic)

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 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 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,

function onClicked(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()

    script.Parent.Sound:Play()-- Copied from first script
end

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.

function onClicked(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()

    local sound = script.Parent.Sound:Clone()
    sound.Parent = playerWhoClicked
    sound:Play()
end

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().

function onClicked(playerWhoClicked)
    script.Parent.BrickColor = BrickColor.Random()

    local sound = script.Parent.Sound:Clone()
    sound.Parent = playerWhoClicked
    sound:Play()
    wait(10)
    sound:Stop()

    sound:Destroy()-- Cleanup, technically optional
end

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!

If I helped, please don't forget to accept my answer.
0
But this is also only locally right? Missy_Idiot 0 — 7y
0
Should be, yes. User#11440 120 — 7y
Log in to vote
0
Answered by 7 years ago
Edited 7 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 — 7y
0
Also, accept the person's answer who helped. User#11440 120 — 7y
0
His answer does contain false information though, and would not play locally. User#11440 120 — 7y
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 — 7y
Log in to vote
0
Answered by 7 years ago

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

local Debounce = false --stops it from running a million times at once
function onClicked(plr) --I changed it to plr because it is shorter, and is what most people use.
if Debounce == false then --checks if it is not running a million times at once

local Debounce = true --makes it think it is running a  millions times at once

Instance.new("Sound", game.Workspace.[plr.Name].Head)
game.Workspace.[plr.Name].Head.Sound.SoundId = 'rbxassetid://123456' -- Replace 123456 with what your audio ID is.
game.Workspace.[plr.Name].Head.Sound.Playing = true

wait(10)

game.Workspace.[plr.Name].Head.Sound.Playing = false

local Debounce = false --makes it think it is not running a million times at once
else --Optianal
warn("Stop clicking me, how would you like it if i clicked you???")
warn("Stop clicking me, how would you like it if i clicked you???")
warn("Stop clicking me, how would you like it if i clicked you???")
warn("Stop clicking me, how would you like it if i clicked you???")
warn("Stop clicking me, how would you like it if i clicked you???")
end
end

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

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 — 7y
0
SH this script did not work :O Missy_Idiot 0 — 7y
0
this is not gonna work by itself, use the instructions in green. SH_Helper 61 — 7y

Answer this question