Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a sound play on Roblox when you press a key on your keyboard?

I want a sound to play when I press P on my keyboard in Roblox. Please help!

1 comment

0

Use UserInputService and when a player press P, play the sound.

2 answers

like this

local UserInputService = game:GetService("UserInputService") -- get userinputservice

local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxasetid://" -- put sound id here

if not sound.IsLoaded then -- wait for sound to load to prevent errors
	sound.Loaded:Wait()
end

UserInputService.InputBegan:Connect(function(input) -- when player presses a key
	if input.KeyCode == Enum.KeyCode.P then -- if the key is p
		sound:Play() -- play the sound
	end
end)

1 comment

0

ay thanks, ima try it

I will also try to answer I guess even tho someone already did. I actually used a model to make a slide script play a sound- nvm just edit this

----- For this to work you must have an audio in workspace, and you must put the name into the code

local player = game.Players.LocalPlayer -----Change the Username here if you just want it to be a specific person.
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key) ------Beginners code so this is spammable.
	if key == "typethekeytoplaythesoundhere" then -------Changeable keybind.
		 Game.Workspace.THEAUDIONAME:Play(1)--------Change AUDIONAMEHERE to the audio in workspace.
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.