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

script that when you press e then it plays a audio?

Asked by 7 years ago
Edited 7 years ago

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

0
put it in a codeblock wookey12 174 — 7y
0
whats a code block yogamer13294456 13 — 7y
0
a code block is a chunk of code defined by what "enclosure" it is in: https://imgur.com/a/PGH1F fanofpixels 718 — 7y

2 answers

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Hello, yogamer13294456!

You have to create the audio block and then try to play it!

Your script should be something like:

01-- Local Script
02local SoundToPlay = 00000000 -- Change the "00000000" to the sound id
03 
04 
05audio = Instance.new("Sound") -- Creates the sound block
06audio.Parent = game.Workspace -- Changes the sound block Parent
07audio.SoundId = "http://www.roblox.com/asset/?id=" .. SoundToPlay -- Changes the sound to play
08 
09function 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
14end
15 
16game:GetService("UserInputService").InputBegan:Connect(onKeyPress) -- Detects player input

Place the script above on the "Starter Player Scripts"

Good Luck with your games!

0
Thanks! yogamer13294456 13 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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:

01local music = game.Workspace.Audio
02 
03function 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
08end
09 
10game.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!

0
I thought Playing is a read-only value. KicksForTricks56 36 — 7y
0
Playing is a property of any sound turbomegapower12345 48 — 7y

Answer this question