I know this part if (Key:lower() == "e") then
then i did this if (Key:lower() == "e") then --- detect the button pressing game.Workspace.Sound:Play() --- plays the sound end
and it doesn't work
im a new scripter/dev
Hello, yogamer13294456!
You have to create the audio block and then try to play it!
Your script should be something like:
01 | -- Local Script |
02 | local SoundToPlay = 00000000 -- Change the "00000000" to the sound id |
03 |
04 |
05 | audio = Instance.new( "Sound" ) -- Creates the sound block |
06 | audio.Parent = game.Workspace -- Changes the sound block Parent |
07 | audio.SoundId = "http://www.roblox.com/asset/?id=" .. SoundToPlay -- Changes the sound to play |
08 |
09 | function onKeyPress(inputObject, gameProcessedEvent) -- Function when player press any key |
10 | if inputObject.KeyCode = = Enum.KeyCode.E then -- Tests if key is equals E |
11 | audio.TimePosition = 0 |
12 | audio:Play() |
13 | end |
14 | end |
15 |
16 | game:GetService( "UserInputService" ).InputBegan:Connect(onKeyPress) -- Detects player input |
Place the script above on the "Starter Player Scripts"
Good Luck with your games!
First put that script in a local script, and then put it in Starter Player Scripts If that doesnt work then use mine which is:
01 | local music = game.Workspace.Audio |
02 |
03 | function onKeyPress(actionName, userInputState, inputObject) |
04 | if userInputState = = Enum.UserInputState.Begin then |
05 | music.TimePosition = 0 --we want it to start at the beggining-- |
06 | music.Playing = true |
07 | end |
08 | end |
09 |
10 | game.ContextActionService:BindAction( "keyPress" , onKeyPress, false , Enum.KeyCode.E) |
PS: Remember to put mine in a local script and StarterPlayerScripts. Also use variables more often they save a lot of time!