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

My keydown function script to play sound doesn't work, any ideas why?

Asked by 7 years ago

this is my current script. so my idea is that if you press f on the keyboard, it will play a sound but it doesn't work. I put this code in a local script and put it into startercharacterscripts.

repeat wait() until game.Players.LocalPlayer
m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "f" then
        local Sound = Instance.new("Sound", game.Workspace)
                 Sound.SoundId = "187137639"
                 Sound:Play()

    end
end)

1 answer

Log in to vote
0
Answered by
4D_X 118
7 years ago

KeyDown is deprecated. Also you can't do the sound id "187137639" it must be "rbxassetid://187137639". Please refer to http://wiki.roblox.com/index.php?title=Keyboard_input But here is a fixed script.. And also the repeat wait is not necessary..

function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.F then
        local Sound = Instance.new("Sound", workspace)
                 Sound.SoundId = "rbxassetid://187137639"
                 Sound:Play()
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
Ad

Answer this question