What to write after local sound =?

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! :wave:

1 Like

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

Remove that line, sound has already been defined.

1 Like

I removed this line and this message appeared это

You used the wrong name of UserInputService variable.
It should be UserInputService not userInputService.

You spelt the UserInputService in line 11 than you did to your variable.

Okay, there seem to be no errors, where do put the script to work?

Local script, In starter GUI, starter Character or starter Player

What line should the UserInputService be on?

I mean that on line 11, you were meant to write userInputService with a capital U. .

the script still doesnt work and i dont know why.Maybe its work in game?(in the published game)

You need to put a sound object in
image

Script still doesnt work.maybe the script is not written correctly.(I mean it might not be well written)

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.

I fixed your script above

1 Like

Define the parent of sound, either doing

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
1 Like

Replace the script with this:

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.

1 Like

Thanks! It helped. I’m just not that experienced programmer.

1 Like

Thanks! It helped. The sound has now appeared.

1 Like