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

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

Asked by 6 years ago

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

Log in to vote
1
Answered by
farizarps 132
6 years ago

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)
1
wow we both submitted our answers at the same time, yours is more cleaner so Cvieyra2test 176 — 6y
0
thanks farizarps 132 — 6y
1
Nice to know that people know keydown() is deprecated! :) turtle2004 167 — 6y
0
:) farizarps 132 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

Answer this question