Enemy Health Detect

im new to scripting is there a way to detect if the enemy health is lower than 100
i tried other Operators it just dont work.

script:

script.Parent.OnServerEvent:Connect(function(plr)
	local player = plr.Character
	local box = game.ReplicatedStorage.Hitbox:Clone()
	local weld = Instance.new("Weld")
	local bound = 1
	box.Parent = player.HumanoidRootPart
	weld.Parent = player.HumanoidRootPart
	weld.Part0 = plr.Character.HumanoidRootPart
	weld.Part1 = plr.Character.HumanoidRootPart.Hitbox
	box.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent.Humanoid.Health <= 100 and bound == 1 then
				print("alive")
				bound = 2
				local speed = hit.Parent.Humanoid
				speed:TakeDamage(5)
			elseif hit.Parent.Humanoid.Health == 25 and bound == 1 then
				print("Dead")
				bound = 2
				local speed = hit.Parent.Humanoid
				speed:TakeDamage(100)
			end
		end
	end)
	wait(0.5)
	bound = 1
	box:Destroy()
	weld:Destroy()
end)
	box.Touched:Connect(function(hit)
		print(hit.Parent) -- to see if the hitbox works and what did you hit
		if hit.Parent:FindFirstChild("Humanoid") then
			print(hit.Parent.Humanoid.Health) -- to see its health
			if hit.Parent.Humanoid.Health <= 100 and bound == 1 then
...

You can check there health with math.max function

Your elseif statement will never run because your health will always be less than 100 on the first if statement

2 Likes

Yeah print statement checks health as well but then he will have a bug of the elseif statement not running because he should swap the elseif line of code with the first if statement line

Local humanoid=hit.Parent:FindFirstChildOfClass(“Humanoid”)
Local health=math.max(0,humanoid.health)

Hi,
you should maybe weld the box directly instead of plr.Character.HumanoidRootPart.Hitbox.

weld.Part1 = box

Also in your second if statement your checking if the health is exactly 25. Is this on purpose? You should check if the health is 25 or lower

elseif hit.Parent.Humanoid.Health <= 25 and bound == 1 then

Make sure that the script.Parent.OnServerEvent is executed before you test it since it wont work before it isnt executed.

oh i did not know that

			if hit.Parent.Humanoid.Health > 25 and bound == 1 then
				print("alive")
				bound = 2
				local speed = hit.Parent.Humanoid
				speed:TakeDamage(15)
			elseif hit.Parent.Humanoid.Health <= 25 and bound == 1 then
				print("Dead")
				bound = 2
				local speed = hit.Parent.Humanoid
				speed:TakeDamage(1)

i did this instead and it work

yeah that works as well instead of using 100

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.