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
COMMUNITY QUESTION
How can you play a sound when you press a key?
2 answers
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)
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
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
I agree with him