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:


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.


Yesterday I had an answer from Peeshavee who gave me the following code:

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)

So I used that, and started building my game. Today I wanted to test my game but there is no sound when I click on it. I think it is because the script says "locally" as in locally on my own computer.... But what I need is that people (random people who are playing my game) can click on the sign and the sign will play 10 sec music. Just to them. So without other players (who are also playing the game) will disturb each other with a million different sounds all at once.

Does someone understands what I am trying to say? And does anyone know if this is even possible?

2 answers

Log in to vote
0
Answered by 8 years ago

wfvj014, I thought your script was the same as the other guy. I just read it again and you said: "copy the sound into the player who clicked" but I don't understand. What do you mean? That I need to watch 24/7 for players to play my game and manually copy sound to all of them or something? I mean I can't do that... So I don't understand what you're saying.

0
:/ User#11440 120 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Here is how I imagine a fix to your problem:

Normal script in somewhere near your click part:

local ClickDetector = PATH_TO_CD

local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Name = "MusicEvent"
RemoteEvent.Parent = game.ReplicatedStorage

ClickDetector.MouseClick:connect(function(playerWhoClicked)
    RemoteEvent:FireClient(playerWhoClicked)
end)

LocalScript in StarterPlayerScripts:

local plr = game.Players.LocalPlayer

local RemoteEvent = game.ReplicatedStorage:WaitForChild("MusicEvent")

local Music = Instance.new("Sound",  plr)
Music.SoundId = PUT_YOUR_ID_HERE

RemoteEvent.OnClientEvent:connect(function()
    Music:Play()
    wait(10)
    Music:Stop()
end)

I guess that'll do.

0
You don't have to use RE. User#11440 120 — 8y
0
http://wiki.roblox.com/index.php?title=FilteringEnabled_with_the_Client-Server_Model Implies that a game according to the client servermodel has sounds handled clientsided Happywalker 185 — 8y

Answer this question