How would i make a thing change at a certain amount of health

trying to make a explosive part that fire gets bigger the more damage it has taken / lower its health is

i currently got this

local TntHumanoid = script.Parent.TntHumanoid
local Fire = script.Parent.TntFire
local Smoke = script.Parent.Smoke

local TNTstate = 4

local HasExploded = false

function Damaged()
	if HasExploded then return end
	local Health = TntHumanoid.Health
	local BaseNum = TntHumanoid.MaxHealth/3
	
	local S3hp = TntHumanoid.MaxHealth
	local S2hp = BaseNum*2
	local S1hp = BaseNum
	
	print(BaseNum)
	
	if Health == TntHumanoid.MaxHealth and TNTstate ~= 4 then
		TNTstate = 4
		
		Fire.Enabled = false
		Smoke.Enabled = false
	end
	
	if Health > S2hp and Health < TntHumanoid.MaxHealth and TNTstate ~= 3 then
		TNTstate = 3
		
		Fire.Enabled = true
		Fire.Heat = 4
		Fire.Size = 7
		Fire.TimeScale = 1
		
		Smoke.Enabled = false
	end
	
	if Health > S1hp and Health <= S2hp and TNTstate ~= 2 then
		TNTstate = 2
		
		Fire.Heat = 5
		Fire.Size = 8
	
		Smoke.Enabled = false
	end
	
	if Health < S1hp and Health > 0 and TNTstate ~= 1 then
		TNTstate = 1
				
		Smoke.Enabled = true
		
		Smoke.Opacity = 0.1
		Smoke.RiseVelocity = 0
		Smoke.Size = 0.1
		Smoke.TimeScale = 1
	end
	
	if Health <= 0 and TNTstate ~= 0 then
		TNTstate = 0
		Explode()
	end
end

TntHumanoid:GetPropertyChangedSignal("Health"):Connect(function() Damaged() end)

It works sometimes, but not consistently enough.

You called the function incorrectly.

At the bottom of the script it should be this:


TntHumanoid:GetPropertyChangedSignal("Health"):Connect(Damaged)

Try this.

Humanoid.HealthChanged:Connect(Damaged)

He called it correctly but not efficiently.

1 Like
local TntHumanoid = script.Parent.TntHumanoid
local Fire = script.Parent.TntFire
local Smoke = script.Parent.Smoke

local HasExploded = false

local function Damaged(Health)
	if HasExploded then return end
	if Health == TntHumanoid.MaxHealth then
		Fire.Enabled = false
		Smoke.Enabled = false
	elseif Health > TntHumanoid.MaxHealth * 2 / 3 then
		Fire.Enabled = true
		Fire.Heat = 4
		Fire.Size = 7
		Fire.TimeScale = 1
		Smoke.Enabled = false
	elseif Health > TntHumanoid.MaxHealth * 1 / 3 then
		Fire.Heat = 5
		Fire.Size = 8
		Smoke.Enabled = false
	elseif Health > 0 then
		Smoke.Enabled = true
		Smoke.Opacity = 0.1
		Smoke.RiseVelocity = 0
		Smoke.Size = 0.1
		Smoke.TimeScale = 1
	elseif Health <= 0 then
		Explode()
	end
end

TntHumanoid.HealthChanged:Connect(Damaged)
1 Like