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

How can you play a sound when you press a key?

Asked by
ax20y 0
2 years ago

Hello, i want to make a script that plays a sound when you press a key but i really dont know how to do it i tried and it didn't worked

2 answers

Log in to vote
0
Answered by 2 years ago

If you already have code to play your sound then you can use this to detect whether the player presses the specific key. Note: This is in a local Script

local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.X --Put you key code where the "X" is.
UIS.InputBegan:Connect(function(Input)
    if Input.UserInputType == Enum.UserInputType.Keyboard then
        if Input.KeyCode == Key then
            --Insert Sound Code
        end
    end
end)
0
I agree with him ghostbettert 30 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Do this:

local UIS = game:GetService("UserInputService")
local SoundI = nil --Replace nil with the sound

UIS.InputBegan:Connect(function(input, gpe)
    if input == Enum.KeyCode.E then --Replace E with the key you want
        SoundI:Play()
    end
end

Answer this question