Hello!
Basically this is a local script whose purpose is to play music when a key is pressed.
This is how it looks:
local KeyPressed = Enum.KeyCode.F
local soundid = "5885680263"
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")
local player = game:GetService("Players")
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://"..soundid
local sound =
userInputService.InputBegan:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == keyPressed then
if not sound.IsPlaying then
sound:Play()
else
sound:Stop()
end
end
end)
I don’t know what to write after local sound =
Bye!
I might be wrong but you already nominated a sound so I find the local sound = unnecessery. Try it without
Edit: I suggest also setting the parent of the sound
local KeyPressed = Enum.KeyCode.F
local soundid = "5885680263"
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")
local player = game:GetService("Players")
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://"..soundid
UserInputService.InputBegan:Connect(function(input,isTyping)
if isTyping then return end
if input.KeyCode == keyPressed then
if not sound.IsPlaying then
sound:Play()
else
sound:Stop()
end
end
end)
I don’t think you are understanding what the script does. You declare variables, like in math, using the keyword local, followed by a name and value. Your problem in this case was that you didn’t type UserInputService correctly. Instead of UserInputService, you typed userInputService, which is not the same as the variable that you declared. Remember that programming is case-sensitive like your password; capitals and lowercases will never be treated as the same.
local sound = Instance.new("Sound",workspace)
--Or, you can set the parent in this way. (I reccomend the bottom method, so you
--Can change properties before the sound instancing into workspace.
sound.Parent = workspace
local UserInputService = game:GetService("UserInputService")
local sound = Instance.new("Sound", workspace)
sound.SoundId = "rbxassetid://5885680263"
UserInputService.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.F then
if sound.IsPlaying then
sound:Stop()
else
sound:Play()
end
end
end)
(LocalScript under StarterPlayer > StarterPlayerScripts)
Edit: What this script does is play a sound of ID 5885680263 (or stop the sound if already playing) when the player presses the ‘F’ key.
If you want the sound to be looped ie. keep replaying until ‘F’ is pressed again, add sound.Looped = true below Line 4.