How do i make a song play and after player died it stops?

script.Parent.Touched:Connect(function(hit)
	if played == false then
		workspace.Sounds.Error404:Play()
		played = true
		wait (2) --This Is delay after first play, you can change it if you want to
		played = true
	end
end)

I already have the code of music playing when a player touches it but after the player dies it still plays, I want it to stop after a player dies but still I cant do it.

2 Likes

i dont even know what i to do.

Inhale Server or Local Script?

If it’s a Server Script, this cannot work

I put the script not the local script in a part. I probably should I have it in the local script but ya. let me change it.

also that is a mistake i just put for no reason.

is Error404 looped or is it a “one time play” audio? Also do you want it to stop playing for everyone or only that player?

What I’d do, is put the LocalScript inside StarterPlayerScripts, and define the Sound outside the Touched event:

local Sound = workspace.Sounds.Error404
local Part = workspace.Part
local HitOnce = false

Part.Touched:Connect(function()
    if HitOnce == false then
        HitOnce = true
        Sound:Play()
        wait(5)
        HitOnce = false
        Sound:Stop()
    end
end)

Could you try this?

it is a looped audio. i don’t know what that had to do with anything but you guys are pros.

the thing is i want the audio to play when they click a specific thing. like megalonovia when they click normal sans and impotent king when they click king multiverse. i cant have them play megalonovia on king multiverse.

Then couldn’t you use a ClickDetector for that?

and that is what i am using. i just want the music to stop playing when they die.

Put a localscript in StarterCharacterScripts to get the player’s character and use the Died event on the humanoid

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

char:WaitForChild("Humanoid").Died:Connect(function()
	workspace.Sounds.Error404:Stop()
end)

Something like this

i said i want the music to play when they click something.

like a part for teleporting them somewhere. I want them to here that music.

I guess you want it to stop for the player. So here’s what i’d do. So, as per your script, when the player hits the block it would start playing. So what we want is that (please correct me if im wrong in some assumption

  1. Once the player touches it and the music starts, we dont want to trigger that even until the player dies and does it again.
  2. When the player dies you want it to stop.

So you could do it in different ways. One of them is listening to the .Died event of the players humanoid.

local pl= game.Players.LocalPlayer
local obj= --part you use (I highly recomend you use :WaitForChild()
local sound= workspace:WaitForChild("Sounds"):WaitForChild("Error404")

local canplay=true

obj.Touched:Connect(function(hit)
	if hit:IsDescendantOf(pl.Character) and canplay then
		canplay=false --We stop it from playing again (for the player) until the player respawns
		sound:Play()
		local con
		con=pl.Character:WaitForChild("Humanoid").Died:Connect(function()
			con:Disconnect()
			canplay=true --Now player will be able to play it again
			sound:Stop()
		end)
	end
end)

^^ put this in a localscript in StarterPlayer.StarterPlayerScripts. I’ve not tested it but it should work.

You’ll probably need to handle the music changes from the local standpoint, inside a LocalScript inside StarterPlayerScripts defining both the Player & Character together

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Part = workspace.TouchablePart
local ConditionalCheck = false
local Sound = workspace.Sounds.Error404

Part.Touched:Connect(function(Hit)
    local HitPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if HitPlayer and ConditionalCheck == false then
        ConditionalCheck = true
        Sound:Play()
    end
end)

Humanoid.Died:Connect(function()
    if ConditionalCheck == true then
        ConditionalCheck = false
        Sound:Stop()
    end
end)

Just put the sound on the character, when it dies, the sound will also be destroyed.

Put this LocalScript inside StarterCharacterScripts.

local Players = game:GetService('Players')

local sound = -- sound object
local character = Players.LocalPlayer.Character

local humanoid = character:WaitForChild('Humanoid')

humanoid.Died:Connect(function()
sound:Stop()
end)

Wait, can’t you just put the Sound inside a part & always play it that way when they get teleported? You’d need to change the MaxActivation/MinActivationDistance properties however

didn’t work. maybe i should stop now.

What I mean is:

  • Place the Sound inside a part from where you get teleported to

  • Set the Playing property to true

  • Change the Max/MinActivationDistance of the Sound properties

It should be able to play in a certain radius depending on how long you want to go with it