How to play a sound on a click of a button

Hi developers, xJxck_yy here.

PS: I am not really a Lua scripter, so keep that in mind.

I have a rocket, and at 8 seconds before launch, 4 Liquid Fuel rocket engines ignite, and let out a flame. This happens when clicking a button in the crew cabin.

script.Parent.MouseClick:Connect(function() local location = script.Parent.Parent.Parent.Parent.Parent["Core Stage (2nd stage)"] location.CoreStageParticle1.ParticleEmitter.Enabled = true location.CoreStageParticle2.ParticleEmitter.Enabled = true location.CoreStageParticle3.ParticleEmitter.Enabled = true location.CoreStageParticle4.ParticleEmitter.Enabled = true

5 seconds later, Solid Rocket Boosters ignite, 3 seconds before launch.
wait(5) local srb = script.Parent.Parent.Parent.Parent.Parent["Solid Rocker Boosters (1st Stage)"] srb.SRBIgnition1.ParticleEmitter.Enabled = true srb.SRBIgnition2.ParticleEmitter.Enabled = true wait(2) game.Workspace.weldpart:Destroy() for i = 0,15000,1 do wait(0.1) game.Workspace.c["3rd Stage"].MainPart8.BodyVelocity.Velocity = Vector3.new(0,i,0) end end)

This works just fine, but I need to know how to add in sound. As soon as the button is pressed and the flames for the Liquid Fueled Engines come to life, I want to play a sound, I have named this CoreStageNoise

5 seconds later, when the Solid Rocket Boosters ignite, I want a sound to play, it is named Rocket launch

After this happens, I want to play another sound named SRB Noise

I need SRB Noise to come on just before Rocket launch ends. Rocket launch can’t be looped, but CoreStageNoise and SRB Noise need to be looped.

The button that is pressed is named Launch

PS: I had @seancool07777, one of my friends, do the scripts mentioned above, but I want to try to do this.

Any help is appreciated,
xJxck_yy

2 Likes

In order to get a sound to play, you need to know the path (location) of the sound first.
(ex. in your case, the particle emitters are location.CoreStageParticle1.ParticleEmitter)

Could you send a screenshot of the hierarchy of your rocket launcher? (explorer tab)

image
That is where the sounds are.

image
That is where the Launch button is.

To play a sound on a click of a button, simply do what’s shown below. I assume you’ve already created the sound and added the AssetId.

sound.Loaded:Wait()

textButton.MouseButton1Down:Connect(function()
     sound:Play()
end)

Hope this helped.

It isn’t a text button, rather it is a button named “Launch” with a click detector in it.

With this, how do I make it be looped or not looped?

Instead, just do: ClickDectector.MouseClick . You can set the sound loop inside the actual sound or enable or disable it inside the script using: sound.Looped = true -- or false .

You can loop sounds by setting the looped property in the Properties tab.

I would not set them in the script, just set the tick box in Properties. No need to use code to do this.

Yes, but I only mentioned that if he needed it for specific times.

Is it this?

sound.Loaded:Wait()

Launch.ClickDetector.MouseClick:Connect(function()
sound:Play(ID HERE)
end)

No, because the sound was preloaded in sound.Loaded:Wait(). You should preset the sound Id inside sound in explorer.

So it is this?

Launch.ClickDetector.MouseClick:Connect(function()
sound:Play(ID HERE)
end)

Sorry If I am being annoying, I dont do lua scripting lol.

Let me sum this up. Its:

sound.Loaded:Wait()

Launch.ClickDetector.MouseClick:Connect(function()
sound:Play()
end)

Theres no need for the id in the script. It should be in the sound.

It would be that, but he wants to set the sound inside the script rather than inside explorer. I would suggest declaring a variable for each sound with the preset id and adding a sound.Loaded:Wait() for each of those sounds in order for there to be no delay in the actual sound playing. But yes, this could work too:

Launch.ClickDetector.MouseClick:Connect(function()
sound:Play(SoundId)
end)
1 Like

With your script this happens:

Well in that case you can do:

local sound = “”–sound id
sound.Loaded:Wait()

Launch.ClickDetector.MouseClick:Connect(function()
sound:Play()
end)

Hope this helps!

You never declared sound in the script.

You need to declare the position of the sound and the parent of click detector.

Its the same as you did before by locating the particle emitters.

Let me know if this works out, and if you need help with anything else!

Hope this helps!

Another issue I am facing is actually welding. I have the rocket sitting on the launch pad. There are 8 main parts, seven of them are for stages/spacecraft, and the last one is to hold all of them together. I have a weldpart on the launchpad, and I use Motor6D Maker to weld it to MainPart8, but the weld deletes itself.

It either deletes itself or the weld straight up doesnt work. All other welds work however

It used to work, but now for some strange reason it doesnt. There arent any malicious scripts or anything like that in there either

1 Like