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

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

Asked by 4 years ago

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

0
Use UserInputService and when a player press P, play the sound. Bin_Latin 2 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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)
0
ay thanks, ima try it SethKingfire 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)

Answer this question