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

Click a Part to enable/disable the sound coming from it?

Asked by 5 years ago
Edited 5 years ago

I was wondering if you guys could help me on making a part play or stop its music when a player clicks it. Similar to when you click the potato in Bus Stop Simulator. Btw I added a clicking sound when you click the part.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
    game.Workspace.BOOM.Sound:Stop()
    game.StarterPlayer.Humanoid.Sound:Stop()

local clickdetector = script.Parent:WaitForChild("ClickDetector")
end)

wait(1)

clickdetector.MouseClick:Connect(function()
    game.Workspace.BOOM.Sound:Stop()
    game.StarterPlayer.Humanoid.Sound:Play()

end)

This is all I can come up with, sadly. Please tell me what I'm doing wrong, It would really help! And yes the part which music is coming off from it is called 'BOOM'.

1 answer

Log in to vote
0
Answered by 5 years ago

Ok, so first off, this can be solved through a single function and the use of an 'if' statement.

What you're currently doing is connecting a function to the click detector's MouseClick event, and then connecting another, separate function to the same event. This means that both functions will run every time you click on the object. They will both stop the sound under the Humanoid, but one will play the sound BOOM and the other will stop it, so I really have no idea what actually happens when you run this.

Also, I'm not sure why you're making this toggle a sound in the StarterPlayer.

The code below is a suggestion on how you could solve this. I'm assuming that you wish to turn off a sound in the Humanoid of the player's current character based on your description, so I changed that line as well, but if that's really not what you wanted you can change it back.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function()
    if workspace["BOOM"].Sound.isPlaying == true then
        workspace["BOOM"].Sound:Stop()
        game.Players.LocalPlayer.Character.Humanoid.Sound:Stop()
    else
        workspace["BOOM"].Sound:Play()
        game.Players.LocalPlayer.Character.Humanoid.Sound:Play()
    end
end)
0
Thank you so much this helped a lot! ImprinintInc 18 — 5y
0
You could just do workspace:WaitForChild('BOOM'):WaitForChild('Sound').Playing = not workspace.BOOM.Sound.Playing Fifkee 2017 — 5y
0
That is not the correct way to play a sound ^ User#24403 69 — 5y
Ad

Answer this question