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

How to check for a change in health?

Hello! I was trying to make an AI but ran into issues checking when it took damage it just wouldnt do anything. After adding a print to it and it still not printing anything, google wasnt any help at all.

I've tried using :GetPropertyChange("Health") with the same result and yes I have been dealing damage on the server side not client side if u think that it the issue. I have even wrote a separate script specially to deal damage. AGAIN no results it just doesnt work... idk why

local Humanoid = script.Parent:WaitForChild("Humanoid")

local OldHealth = Humanoid.Health

local UnderAttack = false

function RunAway()
	print("fired")
	UnderAttack = true
	Humanoid.WalkSpeed = 20
	Humanoid:MoveTo(math.random(0,150), 0, math.random(0,150))
	Humanoid.MoveToFinished:Connect(function()
		Humanoid.WalkSpeed = 16
		UnderAttack = false
	end)
end


function RandomMove()
	Humanoid:MoveTo(script.Parent.PrimaryPart.Position + Vector3.new(math.random(0,100), 0, math.random(0,100)))
	Humanoid.MoveToFinished:Connect(function()
		Humanoid.WalkSpeed = 16
		UnderAttack = false
	end)
end


function Multiply()
	wait(math.random(5,200))
	local Clone = script.Parent:Clone()
	Clone.PrimaryPart.CFrame = script.Parent.PrimaryPart.CFrame:ToWorldSpace(CFrame.new(5,2,0))
end

function Main() 
	if not UnderAttack then
		wait(math.random(10,20))
		RandomMove()
	end
end

while wait() do
	if Humanoid.Health < 1 then
		script.Parent:Destroy()
		break
	end
		Main()
end

script.Parent.Humanoid.HealthChanged:Connect(function()
	print("fun")
	if Humanoid.Health < OldHealth then
		print("RUNNING")
		UnderAttack = true
		RunAway()
		OldHealth = Humanoid.Health
		UnderAttack = false
	end
end)

2 comments

0

try moving "OldHealth = Humanoid.Health" (line 55) below the end (line 57)

0

Had no affect like is said it wasnt even printing anything when the event fires but thanks

1 answer

Try this:

script.Parent:FindFirstChild("Humanoid"):GetPropertyChangedSignal("Health"):Connect(function()
	-- Code here
end)

1 comment

0

Yeah same effect still doesn't work- but thanks for trying :(

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