Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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)

2
codeblock pls abnotaddable 920 — 7y
0
Ok. You know how to use hashtags to make headings, but not already codeblocks? hiimgoodpack 2009 — 7y
0
Do not only post code, or only post an explanation. Both are required to make a good question. hiimgoodpack 2009 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

Ad

Answer this question