Script playing sound whenever daed

  1. What do you want to achieve? stop sound playing whenever you die

  2. What is the issue? I’m not sure how to script that

  3. What solutions have you tried so far? I’ve tried adding stuff to the scripts but didn’t work

local player = game.Players.LocalPlayer
local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local sound = player.PlayerGui:FindFirstChild("LowHealthSound")

while wait() do
	if humanoid.Health <= 20 then
		if sound.Playing == false then
			sound:Play()
		end
	else
		if sound.Playing == true then
			sound:Stop()
		end
	end
end

try waiting for the character to spawn, then check for death with the Humanoid’s .Died function.

player.CharacterAdded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
sound:Play()
end)
end)
3 Likes

not working for me. do I place it under the whole script?

Put this under or above the loop

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

Not working, I placed it below the script and tried and didnt work, and then I added it ontop and didnt work.

Btw, why do you have your sound in PlayerGui? And where is this script situated?

The Script is in StarterCharacterScripts and the sound is in PlayerGui

PlayerGui is a place for player’s UI objects: frames, buttons, etc. Sound should be in ReplicatedStorage. Character is a child of workspace, so you can connect to char by this line

local player = game.Players.LocalPlayer
local character = player.Character

Then you need to get your sound

local RS = game:GetService("ReplicatedStorage")
local sound = RS:FindFirstChild("LowHealthSound")

And then you can play it and stop in function binded to humanoid.Died event.

But sound can also be a child of a part, for example. If you need to play a sound of your dying character, you can put sound into character model.

But I need it so it doesnt play again after I die so cause it plays the sound whenever Im dead

Still Plays the audio after i die…

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local sound = RS:WaitForChild("OOT LowHealth")
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
sound.Parent = player.Character

humanoid.HealthChanged:Connect(function()
	if humanoid.Health <= 20 then
		if sound.Playing == false then
			sound.Looped = true
			sound:Play()
		end	
	else
		if sound.Playing == true then
			sound:Stop()
		end
	end
end)

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

But you need to put sound in ReplicatedStorage
And change sound name in RS:WaitForChild(“OOT LowHealth”)