Help with Take Damage sound!

Hello developers! I am currently trying to make a sound play whenever you take damage from a sound folder, and trying to make it so that if I’m at 15 or less health a custom sound plays when I take damage.
I would also like to make it so that if you have a custom MaxHealth lets say like 500 I want it so that when your at like 150 health the custom sound plays.

Unfortunately, I am not big brain enough for this and I tried my best and if you can help me that would be fantastic!
(Shoutout CoreProgramming for the script)

-- Scripted by CoreProgramming. version 2.0 the old version got leaked not publish my me. Put in StarterPlayer > StarterCharacterScripts

local Char = script.Parent
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
local LastHealth = Humanoid.Health
local SoundsFolder = script:WaitForChild("Sounds")
local LowHealth = script:WaitForChild("LowHealthSound")
local Connection

SoundsFolder.Parent = HumanoidRootPart
SoundsFolder = HumanoidRootPart:FindFirstChild("Sounds")

Humanoid.Died:Connect(function()
	Connection:Disconnect()
end)

Connection = Humanoid.HealthChanged:Connect(function()
	if Humanoid.Health < LastHealth and Humanoid.Health <= 85 then --and (Humanoid.Health / Humanoid.MaxHealth * 100) <= 15 then
		print("kinda full on health")
		--print(Humanoid.Health)
		local Sounds = SoundsFolder:GetChildren()
		local ChosenSound = Sounds[math.random(1,#Sounds)]
		if ChosenSound ~= nil then
			--local e = Instance.new("Part", game.Workspace)
			--ChosenSound.Volume = 1
			ChosenSound:Play()
		elseif  Humanoid.Health < LastHealth and Humanoid.Health >= 15 then 
			if LowHealth ~= nil then
			LowHealth:Play()
				print("critically low")
			end
		end
		
	end
	LastHealth = Humanoid.Health
end)

Capture

1 Like

Local script version:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local SoundsFolder = script:WaitForChild("Sounds")
SoundsFolder.Parent = HumanoidRootPart

local LowHealth = script:WaitForChild("LowHealthSound")
LowHealth.Parent = SoundsFolder

Humanoid.HealthChanged:Connect(function(NewHealth)
	if NewHealth > 15 then
		local Sounds = SoundsFolder:GetChildren()
		local ChosenSound = Sounds[math.random(#Sounds)]
		ChosenSound:Play()
	elseif NewHealth <= 15 then 
		LowHealth:Play()
	end
end)

Server script version:

local _, Character = nil, script.Parent or script.AncestryChanged:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local SoundsFolder = script:WaitForChild("Sounds")
SoundsFolder.Parent = HumanoidRootPart

local LowHealth = script:WaitForChild("LowHealthSound")
LowHealth.Parent = SoundsFolder

Humanoid.HealthChanged:Connect(function(NewHealth)
	if NewHealth > 15 then
		local Sounds = SoundsFolder:GetChildren()
		local ChosenSound = Sounds[math.random(#Sounds)]
		ChosenSound:Play()
	elseif NewHealth <= 15 then 
		LowHealth:Play()
	end
end)
1 Like

image
shouldn’t the local Connection have something after it? like,

local Connection = AVeryImportantValue

Instead of using set values for the music to play, you can get the humanoid’s max health, and check if it’s below a percentage. For example:

local HealthPercentage = Humanoid.Health / Humanoid.MaxHealth -- 0 to 1

In this case where you check if it’s below 15, you could replace it with a decimal. For example:

local HealthPercentage = Humanoid.Health / Humanoid.MaxHealth -- 0 to 1

if HealthPercentage <= 0.15 then
-- Runs only if their health is below 15%
end
2 Likes

Thanks a bunch! I do got another question and it’s that how would I make it so that if I’m dead the sounds volume go to 0 and make it 1 when I’m alive?

1 Like

Volume should be at 1 by default, to mute sounds when the player dies add the following block of code to the script.

Humanoid.Died:Connect(function()
	for _, Sound in ipairs(SoundsFolder:GetChildren()) do
		if Sound:IsA("Sound") then
			Sound:Stop()
		end
	end
end)
2 Likes