Health script works weirdly

edit: game link health test thingy - Roblox
script that plays an anim depending on your health basically is sometimes working (you can play the game yourself) here is the main script:

local char = game.Players.LocalPlayer.Character
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
local anim = char:WaitForChild("Animate")
humanoid.HealthChanged:Connect(function(health)
	while true do
		wait()
	if health <= 50 then
		anim.walk.WalkAnim.AnimationId = "rbxassetid://6541469256"
		anim.idle.Animation1.AnimationId = "rbxassetid://6540817358"
		game.Lighting.ColorCorrection.Saturation = -.5 
		game.Workspace.Sound.Playing = true
		humanoid.WalkSpeed = 12
	elseif health >=50 then
		anim.walk.WalkAnim.AnimationId = "rbxassetid://180426354"
		game.Lighting.ColorCorrection.Saturation = 0
		game.Workspace.Sound.Playing = false
		anim.idle.Animation1.AnimationId = "rbxassetid://180435571"
		humanoid.WalkSpeed = 16
		end
	end
end)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			game.Workspace.Sound.Playing = false
			wait(1)
			game.Workspace.Sound.Playing = false
		end)
	end)
end)

There’s a few things I’m not understanding in your code

Why are you disabling the sound, waiting a second, and then trying to disable it here?

Also why is there a while true do loop in your Healthchanged event?

Try this perhaps?

local char = game.Players.LocalPlayer.Character
local humanoid = char.Humanoid
local currentHealth = humanoid.Health
local anim = char:WaitForChild("Animate")
humanoid.HealthChanged:Connect(function(health)
	if health <= 50 then
		anim.walk.WalkAnim.AnimationId = "rbxassetid://6541469256"
		anim.idle.Animation1.AnimationId = "rbxassetid://6540817358"
		game.Lighting.ColorCorrection.Saturation = -.5 
		game.Workspace.Sound.Playing = true
		humanoid.WalkSpeed = 12
	else
		anim.walk.WalkAnim.AnimationId = "rbxassetid://180426354"
		game.Lighting.ColorCorrection.Saturation = 0
		game.Workspace.Sound.Playing = false
		anim.idle.Animation1.AnimationId = "rbxassetid://180435571"
		humanoid.WalkSpeed = 16
	end
end)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			game.Workspace.Sound.Playing = false
			wait(1)
			game.Workspace.Sound.Playing = false
		end)
	end)
end)

Another thing I saw was that you were checking health is less than or equal to 50 and then an elseif for checking it’s greater than or equal to 50 when I think it’s better if you just did an else

1 Like

What is this for? Why are you disabling the sound twice even though its disabled.

You don’t need a while true do loop, this is probably why the script works only sometimes.

1 Like

the sound disabling thing is just a fix for a sound playing after playing death, thanks.

1 Like

Anytime! It was a bit odd to see a while true do loop there since it doesn’t really need to be there since HealthChanged runs when your health changes of course. If you have anymore issuess don’t be afraid to make another post!

i guess i didnt know that a while true do loop can cause that, everything is working fine now though, thanks

I accidentally edited my post instead of sending a new one to say my usual for when I help someoen with their problem haha. But again, anymore issues, don’t be afraid to make another post!

1 Like

really quick in the if health <= 50 then i added
game.Workspace["Winds of Fjords"].PitchShiftSoundEffect.Octave = .5 and it doesnt seem to work, no errors btw

Hmm, maybe put the Sound in replicatedStorage and change the code around? A few things

Is the audio playing already?
May I see your current code?

1 Like

yes the audio is playing i guess you could say as background music, can you not change pitch while the sound is playing?

local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
local anim = char:WaitForChild("Animate")
humanoid.HealthChanged:Connect(function(health)
	
	if health <= 50 then
		anim.walk.WalkAnim.AnimationId = "rbxassetid://6541469256"
		anim.idle.Animation1.AnimationId = "rbxassetid://6540817358"
		game.Lighting.ColorCorrection.Saturation = -.5 
		game.Workspace.Sound.Playing = true
		humanoid.WalkSpeed = 12
		game.Workspace["OoT Low Health"].Playing = true
		game.Workspace["Winds of Fjords"].PitchShiftSoundEffect.Octave = .5
	else
		anim.walk.WalkAnim.AnimationId = "rbxassetid://180426354"
		game.Lighting.ColorCorrection.Saturation = 0
		game.Workspace.Sound.Playing = false
		anim.idle.Animation1.AnimationId = "rbxassetid://180435571"
		humanoid.WalkSpeed = 16
		game.Workspace["OoT Low Health"].Playing = false
		game.Workspace["Winds of Fjords"].PitchShiftSoundEffect.Octave = 1
	end
end)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			game.Workspace.Sound.Playing = false
			wait(1)
			game.Workspace.Sound.Playing = false
		end)
	end)
end)

Hmm…Maybe try waht I said and move “Winds of Fjords” to ReplicatedStorage and change the lines to reference it there?

1 Like

i dont know how that worked but it did

1 Like

i did end up having too duplicate it though

1 Like

Hmm, at least it’s working though right? What do you mean by duplicate it?

1 Like

same sound but one is in workspace and one is in replicated storage, anyways, how could i put another if statement, like if the players health is less then 10 else, then basically the other code for less than 50, then after you heal from that, the else statement for when your health is above 50

Just use an elseif,

local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
local anim = char:WaitForChild("Animate")
humanoid.HealthChanged:Connect(function(health)
	if health <= 10 then
		--Code	
	elseif health <= 50 then
		anim.walk.WalkAnim.AnimationId = "rbxassetid://6541469256"
		anim.idle.Animation1.AnimationId = "rbxassetid://6540817358"
		game.Lighting.ColorCorrection.Saturation = -.5 
		game.Workspace.Sound.Playing = true
		humanoid.WalkSpeed = 12
		game.Workspace["OoT Low Health"].Playing = true
		game.Workspace["Winds of Fjords"].PitchShiftSoundEffect.Octave = .5
	else
		anim.walk.WalkAnim.AnimationId = "rbxassetid://180426354"
		game.Lighting.ColorCorrection.Saturation = 0
		game.Workspace.Sound.Playing = false
		anim.idle.Animation1.AnimationId = "rbxassetid://180435571"
		humanoid.WalkSpeed = 16
		game.Workspace["OoT Low Health"].Playing = false
		game.Workspace["Winds of Fjords"].PitchShiftSoundEffect.Octave = 1
	end
end)
```
1 Like