How to make a keybind play a sound?

I have a sound that enables a particle when a key is pressed, but I want to make it so it activates a sound instead, and plays the entire length of the sound instead of cutting off.

Is there any way I can transform these scripts into one that makes a sound play with a keybind?

SERVER SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("SoundStart")
local particles = game.Workspace.Orb1

remote.OnServerEvent:Connect(function()
	for number,index in pairs(particles:GetChildren()) do
		if index:IsA("ParticleEmitter") then
			index.Enabled = not index.Enabled
		end
	end
end)

CLIENT SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local userInputService = game:GetService("UserInputService")
local remote = ReplicatedStorage:WaitForChild("SoundStart")

userInputService.InputBegan:Connect(function(input, gp)
	if gp then return end

	if input.KeyCode == Enum.KeyCode.K then
		remote:FireServer()
	end
end)
2 Likes

Is there a separate script where the sound is played whenever a particle is enabled?

2 Likes

No, this script is meant for particles, I just want to know how I can make it play a sound instead

Basically the same keybind will enable the particle and sound in the same part.

1 Like

Do you want the sound to start and the particles enabled simultaneously, or one ends and the other begins?

1 Like

Yes, both the particle and sound should enable at the same time.
(Particle should be a burst as well, not continuous)

1 Like

Final question: Do you want it to be played for all players, or just the player who activates the keybind?

1 Like

It should be for all players since it has RemoteEvents in it. (not sure if that lets sound be played for everyone)

1 Like

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("SoundStart")
local particles = workspace.Orb1
local sound = workspace -- Wherever you have your sound object in! 

remote.OnServerEvent:Connect(function()
	if (sound.Playing == false) then sound:Play() end -- Remove the if statement when you're not concerned with the sound being spammed.
	spawn(function()  -- I like to use spawn(callback) since it uses less line space. I know it's not necessarily the best practice.
		for number,index in pairs(particles:GetChildren()) do
			if index:IsA("ParticleEmitter") then
				index.Enabled = not index.Enabled
			end
		end
	end)
end)

Your client script should remain the same.

1 Like

If you need to create a Sound instance for use, then you could define “sound” as this:

local sound = Instance.new("Sound", game.Players.LocalPlayer.PlayerGui)
sound.SoundId = "rbxassetid://" -- Insert the id here.
sound.Looped = false
1 Like

Does this do the same thing as putting a sound in the part?
(Basically, does it also allow everyone to hear it?)

Different problem, not sure why the audios I’m going to upload keep saying “Asset creation failed”

Do audios need to be above a certain length? Or is it just roblox itself.

First question: Yes, it should allow everyone to hear it!
Second question: You’ll likely need to grant access to use the audio via the audio’s configuration page if it isn’t considered as a Sound Effect by Roblox’s standard. As of right now, you’ll be unable to use any audio that isn’t uploaded by you and isn’t freely distributed as a sound effect.

1 Like

For the second question, I’m trying to upload an mp3 file that I made specifically for what I’m trying to do, but won’t upload.

Try using Audacity to convert it into a .ogg file, and attempt to upload the asset again! I’ve had that problem a few times myself, but converting it to an .ogg file seems to work.

Unfortunately on this Mac, I can’t upload m4a files into Audacity without a certain library that it can’t locate even if I downloaded it.

I’m not sure how else to turn it into an ogg file.

If the library is ffmpeg, then there should be an official download on their official site. If all else fails, then you could try to use an online m4a => ogg converter.

Ok! Sound actually works!

Another issue though, the particles are continuous, they should just emit once. (Also counting the “EmitCount” attribute that a plugin I’m using adds)

Before reading the code, there’s no need for server and client here, detect input from client and play the sound from the client.

(That’s in case you want the sound to be played for one player only.)

They want it played for all players, though. The only solution would have to use remote events in order to fire to the server and play a sound for all players.

Is there a way to only have the particles activate once instead of just being enabled?

Press the keybind, particles get emitted once. (Holding won’t activate them either, it waits for another press)

Cause at the moment it enables it permanently when I press a key.