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

Help Play Sound When KeyDown? And Stop when clicked again

Hello Everyone so im getting better and better at scripting in days but i still dont know how to make that if you click Q button in your keyboard and its be detected by roblox so its play sound i tried like this but its dont work.. Help Play Sound When KeyDown? And Stop when clicked again

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.KeyDown:connect(function(key)
    if key ==  "q" then
game.Workspace.Sounds.MENU:play()

I tried this script in localScript,And Normal Script.. Thanks If you will help me! And good luck.

3 comments

0

did you put 'end)' at the end of the script?

0

nop

0

but its still dont works.

1 answer

✓ Accepted answer

.KeyDown is depracated, it shouldn't be used anymore. Instead we have a service called UserInputService that handles all input including key presses with the InputBegan event.

http://wiki.roblox.com/index.php?title=API:Class/UserInputService

Local Script

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Q then
			--This code will fire everytime Q is pressed, so put your sound code here
		end
	end
end)

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