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 6 years ago
Edited 6 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 — 6y
0
whats a code block yogamer13294456 13 — 6y
0
a code block is a chunk of code defined by what "enclosure" it is in: https://imgur.com/a/PGH1F fanofpixels 718 — 6y

2 answers

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

Hello, yogamer13294456!

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

Your script should be something like:

-- Local Script
local SoundToPlay = 00000000 -- Change the "00000000" to the sound id


audio = Instance.new("Sound") -- Creates the sound block
audio.Parent = game.Workspace -- Changes the sound block Parent
audio.SoundId = "http://www.roblox.com/asset/?id=" .. SoundToPlay -- Changes the sound to play

function onKeyPress(inputObject, gameProcessedEvent) -- Function when player press any key
    if inputObject.KeyCode == Enum.KeyCode.E then -- Tests if key is equals E
        audio.TimePosition = 0
        audio:Play()
    end
end

game: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 — 6y
Ad
Log in to vote
0
Answered by 6 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:

local music = game.Workspace.Audio

function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        music.TimePosition = 0 --we want it to start at the beggining--
        music.Playing = true
    end
end

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!

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

Answer this question