How do I make a system that stops the music when you die?

I am making the system that stops the music when you die and plays the music when you respawn, but I don’t know how.

The local script parented at StarterPlayerScripts:

local Player = game.Players.LocalPlayer
local humanoid = Player.Character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	game.Workspace.music:Stop()
	game.Workspace.BEWORRYDONTHAPPY:Stop()
	game.Workspace.dvd:Stop()
	task.wait(3)
	game.Workspace.music:Play()
end)

Any help is appreciated, thanks.

2 Likes

you gonna have to wait for the character to load in, sometimes you would end up dying again in under 3 seconds and the music will play before you are loaded in as if the code hasnt finished yet from the first death. That is how events work, they run code entirely on a new thread or “script” of their own

local Player = game.Players.LocalPlayer
local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")

	humanoid.Died:Connect(function()
		game.Workspace.music:Stop()
		game.Workspace.BEWORRYDONTHAPPY:Stop()
		game.Workspace.dvd:Stop()

	end)
	--music plays after you are loaded in, note this is Not when you die
	task.wait(3) 
    if not game.Workspace.music.Playing then
		game.Workspace.music:Play()
	end
end
Player.CharacterAdded:Connect(onCharacterAdded)
onCharacterAdded() 

It works, but when I teleport to another part, the output says this: "Fade" is not a valid member of ScreenGui "TeleportGui".

The local script used to teleport to:

local Player = game.Players.LocalPlayer
local EnterSwanEvent = game.ReplicatedStorage.Events:WaitForChild("EnterSwanEvent")
local EnterDVDEvent = game.ReplicatedStorage.Events:WaitForChild("EnterDVDEvent")
local TeleportGui = Player.PlayerGui:WaitForChild("TeleportGui")
local TweenService = game:GetService("TweenService")
local MapToTp1 = game.Workspace:WaitForChild("SwanLocationMap")
local MapToTp2 = game.Workspace:WaitForChild("DVDMANMap")

EnterSwanEvent.OnClientEvent:Connect(function()
	TweenService:Create(TeleportGui.Fade, TweenInfo.new(0.5), {BackgroundTransparency = 0}):Play()
	task.wait(0.9)
	game.Workspace.music:Stop()
	game.Workspace.BEWORRYDONTHAPPY:Play()
	for i, v in pairs(Player.Character:GetChildren()) do
		if v:IsA("BasePart") then
			v.CFrame = MapToTp1.SwanToTeleport.CFrame
		end
	end
	TweenService:Create(TeleportGui.Fade, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
end)

EnterDVDEvent.OnClientEvent:Connect(function()
	TweenService:Create(TeleportGui.Fade, TweenInfo.new(0.5), {BackgroundTransparency = 0}):Play()
	task.wait(0.9)
	game.Workspace.music:Stop()
	game.Workspace.dvd:Play()
	for i, v in pairs(Player.Character:GetChildren()) do
		if v:IsA("BasePart") then
			v.CFrame = MapToTp2.DVDToTeleport.CFrame
		end
	end
	TweenService:Create(TeleportGui.Fade, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
end)

You are now going off topic from your question, please learn some basic scripting knowledge such as referencing a variable to an instance, using waitforchild() to wait to load instance so it won’t error and halt the entire code, using findfirstchild() to determine if the instance exists etc because now your problem could be solved by just doing that. Do mind where you put this Fade instance is located in the Explorer tab, is it being cloned in by another script to be inside TeleportGui under some condition?