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

How would I make music play when a door is touched?

When a door is touched in my game, I want music to play:

--Vaiables
local Sensor = script.Parent.Sensor
local Door = script.Parent["Prison Side Door 1"]
local Window = script.Parent.Window
local Player = game.Players.LocalPlayer
local Light = script.Parent.Light

--make the sound ID
local music Instance.new("Sound",Player)
music.SoundId = "rbxassetid://993959716"
music.Name = "AMusic"

--Function for everything

local function Everything(part)
	local hum = game.Workspace:FindFirstChild("Humanoid")
	print(hum)
	music:Play()
end

Sensor.Touched:Connect(Everything)

When run It gives an error:

14:38:13.419 - Workspace.Regions.City.Prison.Police Station.Floor 1.Doors.Prison Side Door 1.The Door Opener:10: attempt to index local 'music' (a nil value)

Can someone help me with the error?

1 comment

0

local music Instance.new("Sound",Player) should be local music = Instance.new("Sound",Player) ur missing an '='

1 answer

✓ Accepted answer
--Vaiables
local Sensor = script.Parent.Sensor
local Door = script.Parent["Prison Side Door 1"]
local Window = script.Parent.Window
local Player = game.Players.LocalPlayer
local Light = script.Parent.Light

--make the sound ID
-- You forgot to put the "=" after "music".
local music = Instance.new("Sound",Player)
music.SoundId = "rbxassetid://993959716"
music.Name = "AMusic"

--Function for everything

local function Everything(part)
    local hum = game.Workspace:FindFirstChild("Humanoid")
    print(hum)
    music:Play()
end

Sensor.Touched:Connect(Everything)

Forgetting to put a symbol is very common in coding. I put a comment on where you caused the script to error.

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