Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Having troubles with a pain script, including not playing any sound. Can someone help?

Hello, everyone. I am currently trying to make a pain script that will play a pain noise upon taking a certain amount of damage, as long as your health isn't 0. Also, there are no errors in the output. My code is listed below and help will be much appreciated.

Script is located in game.StarterPlayer.StarterCharacter, so it's already inside of the character upon the game starting.

--//Pain noises script
CP = game:GetService("ContentProvider")
CP:Preload("rbxassetid://155675172") -- Weak
CP:Preload("rbxassetid://455247628") --Medium
CP:Preload("rbxassetid://455247407") -- Major 

local currHealth = script.Parent.Humanoid.Health
local Dam = 0
--------------------------------------------------------------------
MinorPain = 20
MediumPain = 25
Major = 45




function checkHealth()
--------------------------------------------------------------------
if currHealth - Dam < MinorPain then --Only plays when you take less than 20 damage
script.Parent.Weak:Play()
wait(1)
script.Parent.Weak:Stop()
--------------------------------------------------------------------
if currHealth - Dam < MediumPain > MinorPain then  --Only plays when you take more than 25 damage
script.Parent.Medium:Play()
wait(1)
script.Parent.Medium:Stop()	
--------------------------------------------------------------------
if currHealth - Dam < Major > MediumPain then --Only plays when you take more than 45 damage
script.Parent.Major:Play()
wait(1)
script.Parent.Major:Stop()	
--------------------------------------------------------------------
end
end
end
end



while true do
	wait(0)
	checkHealth()
end

1 answer

You had the correct idea about where to put this script but the operators do not work this way, e.g. currHealth - Dam < Major > MediumPain as all relational operators return a boolean meaning that [[currHealth - Dam <] returns a boolean] > some value will error as you are trying to compare a boolean to a value.

It is best to use an event over a loop where possible, for this you should use HealthChanged which also gives an example of how to determine the change in health.

Lastly as you are not using FE when playing sound this way it will play for all players, to resolve this you can use PlayLocalSound as this does not depend on the need for local parts / FE.

Putting this all together:-

local soundServ = game:GetService('SoundService')
local cp = game:GetService("ContentProvider")

-- we should only need to load these once
CP:Preload("rbxassetid://155675172") -- Weak
CP:Preload("rbxassetid://455247628") --Medium
CP:Preload("rbxassetid://455247407") -- Major

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local curHelath = humanoid.Health

humanoid.HealthChanged:Connect(function(newVal)
	local changedVal = curHelath - newVal
	
	if changedVal > 0 then
		if changedVal <= 20 then
			soundServ:PlayLocalSound(script.Parent.Weak)
		elseif changedVal <= 25 then
			soundServ:PlayLocalSound(script.Parent.Medium)
		elseif changedVal <= 45 then
			soundServ:PlayLocalSound(script.Parent.Major)
		end
	end
end)

I hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.