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

Sound plays when key is pressed?

Asked by 5 years ago

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

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

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

(location of the audio):Play()

Ad
Log in to vote
0
Answered by
3wdo 198
5 years ago

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)
Log in to vote
0
Answered by 5 years ago

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)

Answer this question