I want to have music that stops playing once you touch a part, and after a few seconds, it plays again

It’s saying nil in this case because it never found the sound and at this point music is nil.
Make sure your sound is where you’re saying and spelled exactly the same way and is a valid working sound. Also in this case use WaitForChild vs FindFirstChild.

In fact a FindFirstChild could have even set the load music in motion but, didn’t give it time to load by the point it hit music:Stop() … Who knows. Either way WaitForChild is the way to go here.


local music = game.SoundService:WaitForChild (“Music”)

Green_Noob’s script works fine. It’s too simplistic to fail no matter how you call the child.
Something else is wrong here. Check that sound for name and volume.
Is this a massive track that takes longer to load than your test it time?
Can you play the sound at all? (even with a click from the studio)

image
are there anything that needs to be edited?

it’s a song of 200+ seconds, it should be stopped when touching the part until the cooldown ends

That’s huge. Them take time to load. Is the sound playing before you touch the part?

yes, it’s music of a said spaceship, and when you touch a part, a blackhole will do something, which turns all the lights off

And this sound is the only one of it in your program? You started this sound in game.SoundService?

it’s in the soundservice, as the spaceship is quite large

Well, you’re messing with black holes and now you have broken the fabric of time.
I don’t know what else to say … the script works for me with a sound in the same spot.

Basically, the ship has music playing, and when you touch a part (basically you touch it with your foot) somewhere on the ship, the blackhole does something which turns the power off, but the part doesn’t turn the music off once walking over said part

nevermind, i’ve found out the music was in a folder, and not seperate, it works

Sorry for not replying, I’m glad it works now!

Ok, I knew there was something else going on. Now let’s refine that script a bit so I don’t leave you with your next headake.

Well that was pretty fail, my bad. A slight adjustment. This will not only work out totally, it will only stop the sound for the player that touched it.

– LocalScript in StarterPlayerScripts or in StarterGui

task.wait(1)

local db=true
local part=workspace:WaitForChild("Part") -- where is this part!
local music=game.SoundService:WaitForChild("Music")
function partTouched(otherPart,objectPart)
	if otherPart.Parent:FindFirstChild("Humanoid")~=nil then
		if(db)then db=false
			music:Stop() wait(40)
			music:Play() db=true
		end
	end
end
part.Touched:Connect(partTouched)

Now that should really cover it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.