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

Sound plays when key is pressed?

I would like it so when a key is pressed a sound is played and only the player who pressed the key can hear the sound? How would I do this?

This is the current key detection script I have:

local Player = game.Players.LocalPlayer local Mouse = Player.GetMouse()

Mouse.KeyDown:connect(function(Key) Key = Key:lower() if Key == 'a' then

end

end)

3 answers

Keep the sound somewhere local like in the player's StarterPack then do

(location of the audio):Play()

idk where you sound was but im just gonna assume its in the workspace:

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.A then
		game.Workspace.Sound:Play()
	end
end)

Use UserInputService since detecting the key pressing by using :GetMouse() is old.

UserInputService allows for way more types of inputs by the players, to much to all list here but here is a page of UserInputService

https://developer.roblox.com/en-us/api-reference/class/UserInputService

input is the type of input that the player had inputted

event determines if the if the game had processed the event or not

local UIS = game:GetService("UserInputService")
local KeyCode = Enum.KeyCode.A

UIS.InputBegan:Connect(function(input, event)
	if input.KeyCode == KeyCode then
		workspace.Sound:Play()
	end
end)

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