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

How do you make a script that plays a sound when a hot key is pressed?

What does a script look like that when a hot key is pressed e.g “k”, a sound is played but it is a local sound that is emitted from the player?

2 answers

✓ Accepted answer

You would need to add a Sound object to the player.

use UserInputService to get when the player presses a key, then play the sound.

sound = WhereEverTheSoundIs

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.(whatever) then
		sound:Play()
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

4 comments

1

wow we both submitted our answers at the same time, yours is more cleaner so

0

thanks

1

Nice to know that people know keydown() is deprecated! :)

0

:)

Accept farizarps' answer lol, ours are both basically the same, I didn't include "gameProcessedEvent" because I thought it was pointless if it aint used in a function.

Well to get keybinds, you need a localscript, so the local sound part is already covered. Unless what you mean by that is a sound that is played GLOBALLY (heard by everyone) and not just for the player.

UIS = game:GetService("UserInputService") -- UserInputService
local audio = whereversoundis
UIS.InputBegan:connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.R -- Change R to whatever key
		audio:Play()
	end
end)

I've never personally ever used keys, so I just copied & rewrote from the wiki: https://developer.roblox.com/articles/Keyboard-Input-Methods

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