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

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

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

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)

1 comment

0

I agree with him

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.