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

This is supposed to change music depending on your health. Why doesn't it work in play mode?

Here's my code.

local humanoid = game.Players.LocalPlayer.Character.Humanoid local oldHealth = humanoid.Health humanoid.HealthChanged:connect(function(health) if oldHealth > 30 and health <= 30 then game.Workspace.LowHealth:Stop() game.Workspace.Criticalhealth:Play() elseif oldHealth < 30 and health >= 30 then game.Workspace.Criticalhealth:Stop() game.Workspace.LowHealth:Play() end oldHealth = health end)

3 comments

2

codeblock pls

0

Ok. You know how to use hashtags to make headings, but not already codeblocks?

0

Do not only post code, or only post an explanation. Both are required to make a good question.

1 answer

I have done some tweaking to your variables. First why did I add events? Well, I did that so it could wait till the player and character join instead of automatically getting it (If we were to automatically get it, and the player or character didn't add, it would return nil). By the way, connect is deprecated, use Connect I stored the sounds inside a table so you could easily access it. I also didn't find the variable, oldHealth very useful since the argument in the HealthChanged event is the amount of health they have.

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait() -- Gets player from game.Players or Waits till the player adds
local char = player.Character or player.CharacterAdded:Wait() -- Gets Character from player or Waits till the character adds
local humanoid = char:WaitForChild("Humanoid")  
local sounds = {
	workspace.LowHealth,
	workspace.CriticalHealth
}
humanoid.HealthChanged:Connect(function(health)
	if health <= 30 then 
		sounds[1]:Stop() 
		sounds[2]:Play() 
	elseif health >= 30 then
		sounds[2]:Stop() 
                sounds[1]:Play() 
	end 
end)

If this answer fixed your problem, please accept it, if not, please ask questions Please use the code block(the Lua button), and put your code inside(and please indent), it makes things readable

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