Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

0

put it in a codeblock

0

whats a code block

0

a code block is a chunk of code defined by what "enclosure" it is in: https://imgur.com/a/PGH1F

2 answers

✓ Accepted answer

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!

1 comment

0

Thanks!

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!

2 comments

0

I thought Playing is a read-only value.

0

Playing is a property of any sound

This discussion is preserved as it was. New answers, comments, and votes are closed.