Play Music when a part is touched And stop when another part is touched

So im making a game chase sequence when the sequence starts a music plays when it ends the music stops that is what im trying to do
heres the sound play script

debounce = false

script.Parent.Touched:connect(function(hit)
if not debounce then
debounce = true
if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local sound = script.Parent.Sound:Clone()
sound.Parent = player.PlayerGui
sound:Play()
wait(60)
end
debounce = false
end
end)

here is the sound stop script

script.Parent.Touched:connect(function(hit)
	if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
		local sound = stop
	local sound = script.Parent.Sound1:Clone()
	sound.Parent = player.PlayerGui
	end
end)

pls help

1 Like

Why did you define sound twice? What is stop?

Also you aren’t making the debounce available again for the player.

To play music when touching a part, you could start by doing:

Make SoundGroup inside of SoundService, then add Sound inside group.
image

You would also need to create a new local script inside of StarterPlayerScripts,
image
then to then paste in this code:

-- service
local soundService = game:GetService("SoundService")

-- variables
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- touch event
game.Workspace.Part.Touched:Connect(function(hit)
	-- check if player touched the part
	if character == hit.Parent then
		-- loop through sound group and disable all sounds
		for _, sound in pairs(soundService.SoundGroup:GetChildren()) do
			sound:Stop()
		end
		-- play sound that you want
		soundService.SoundGroup.Sound:Play()
	end
end)

Haven’t tested the code, so you can do just that.
And if didn’t work then try solve the issue…

İt worked But i want the sound to stop the sound then play another sound when a specific part is touched like in doors seek chase end sequence

1 Like

Then utilise my code and do just what you want…

1 Like

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