How can I make this conditional code work?

I have this bandage system that plays three different animations depending on the health of the user. I’m not sure how I can make it so that they don’t “overlap”. What I mean by overlap is that I’m using “less than or equal to” symbols to detect the player’s health and decide what animation to play. The problem is that they play over each other. For example, if the player is at 20 hp which is the minimum health lost for the heavy healing animation, they will play all the animations for medium damage (60 hp) and light damage (80). How can I fix this?

Code:

local Tool = script.Parent -- General setup
local Durability = 2 -- two uses
local ResetTime = 4.5 -- cooldown to using bandage
local Player = game.Players.LocalPlayer

local StarterGui = game:GetService("StarterGui")

local Equip = Tool:WaitForChild("EquipAnimation") -- Animations setup
local HoldBandage = Tool:WaitForChild("HoldBandageAnimation")
local OpenBandage = Tool:WaitForChild("OpenBandageAnimation")
local LightInjury = Tool:WaitForChild("LightInjuryAnimation")
local MediumInjury = Tool:WaitForChild("MediumInjuryAnimation")
local HeavyInjury = Tool:WaitForChild("HeavyInjuryAnimation")

local isHealing = false  -- Declare debounce variable
local canHeal = false

local BandageEvent = script.Parent.Events:WaitForChild("BandageEvent") -- events set up
local OpenBandageEvent = script.Parent.Events:WaitForChild("OpenBandageEvent")

local Character = Player.Character 
local Humanoid = Character:WaitForChild("Humanoid")

local HoldBandageAnimation = Humanoid:LoadAnimation(HoldBandage)
local EquipAnimation = Humanoid:LoadAnimation(Equip)	


function onEquipped(Mouse) -- Equip Line	
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) -- disable inventory			
EquipAnimation:Play()
canHeal = false	
wait(1.3)
HoldBandageAnimation:Play()	
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) -- enable inventory				
canHeal = true
end


function onUnequipped(Mouse) -- Unequip Line
	local Character = Player.Character 
	local Humanoid = Character.Humanoid
	
	EquipAnimation:Stop()
	HoldBandageAnimation:Stop()
end


function onActivated()
	if Character.BleedingScript.Disabled == true then
		canHeal = false
	end
	
	if not isHealing and canHeal then  -- Check that debounce variable is not true
		isHealing = true  -- Set variable to true
		canHeal = true
		
		local Character = Player.Character -- play animation
		local Humanoid = Character:WaitForChild("Humanoid")
		
		local OpenBandageAnimation = Humanoid:LoadAnimation(OpenBandage)	
		local LightInjuryAnimation = Humanoid:LoadAnimation(LightInjury)
		local MediumInjuryAnimation = Humanoid:LoadAnimation(MediumInjury)
		local HeavyInjuryAnimation = Humanoid:LoadAnimation(HeavyInjury)

		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) -- disable inventory	
		
		OpenBandageEvent:FireServer()
		OpenBandageAnimation:Play()
		
		wait(0.85)
		
		local nextDurability = Durability -1 --- durability line
		print(nextDurability)
		Durability = nextDurability	
		
		if Humanoid.Health <= 80 then 
		BandageEvent:FireServer()
		LightInjuryAnimation:Play()
		print("Heal Light")	
		
		if Humanoid.Health <= 40 then
		BandageEvent:FireServer()
		MediumInjuryAnimation:Play()	
		print("Heal Medium")				
				
		if Humanoid.Health <= 20 then		
		BandageEvent:FireServer()
		HeavyInjuryAnimation:Play()			
		print("Heal Heavy")			
				
				end
			end			
			
		wait(ResetTime)  -- Wait for reset time duration
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) -- enable inventory				
			
		isHealing = false  -- Reset variable to false
		canHeal = true
					
		if Durability == 0 then --- destroy after durability runs out		
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) -- enable inventory				
				
		EquipAnimation:Stop() -- stops all animations
		HoldBandageAnimation:Stop()	
		OpenBandageAnimation:Stop()		
		LightInjuryAnimation:Stop()
		MediumInjuryAnimation:Stop()
		HeavyInjuryAnimation:Stop()
				
		print("Tool broke")
		script.Parent.Handle:Destroy() -- destroys handle as well to stop the hold animation from playing
		Tool:Destroy()	
					end
				end
			end
		end
	
script.Parent.Activated:Connect(onActivated)
script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(onUnequipped)

Example in output:
pain

1 Like

You do an elseif thing:

local number = math.random(1,500)

if number <= 10 then
--
elseif number <= 20 then
--
elseif number <= 100 then
--
end

You go least to greatest. I deleted my last post because I didn’t quite understand what you said but now I know.

1 Like

Like this?


		if Humanoid.Health <= 20 then 
		BandageEvent:FireServer()
		HeavyInjuryAnimation:Play()
		print("Heal Heavy")	
		elseif Humanoid.Health <= 40 then
		BandageEvent:FireServer()
		MediumInjuryAnimation:Play()	
		print("Heal Medium")				
		elseif Humanoid.Health <= 80 then		
		BandageEvent:FireServer()
		LightInjuryAnimation:Play()			
		print("Heal Light")				

I just reversed everything.

1 Like

you do elseif instead of more if statements

Edit: when one of these elseif/if statements run, it will break out of the while if statement
Edit2: yes you got it right

1 Like