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

Why wont my blood script detect when an enemy drops below 50 health?

This is a really simple script that I think doesn't work because if statements don't update all the time, but I don't know how to make it so it does. Please help if you do.

if script.Parent.Humanoid.Health <= 50 then
	script.Parent.Torso.ParticleEmitter.Enabled = true
end

2 answers

✓ Accepted answer

What you should do is use an event called HealthChanged. HealthChange is an event from the Humanoid that detects whenever the health of the humanoid, well, changes.

local humanoid = script.Parent.Humanoid

--Assign an event
humanoid.HealthChanged:Connect(function(newHealth)
	if newHealth <= 50 then
		script.Parent.Torso.ParticleEmitter.Enabled = true
	else
		script.Parent.Torso.ParticleEmitter.Enabled = false
	end
end)

What the script does is that whenever the health of the humanoid changes, it detects whether if the health is lower than 50, it it is, it will enable the particle emmiter. I also assume that once the humanoid's health is greater than 50, you would want the particle emitter to disable itself, but if you don't want it to, just delete the else part of the if-statement.

you just showed three lines of code so i am assuming that you forgot to constantly check for if the health is below 50

if script.Parent.Humanoid.Health <= 50 then
    script.Parent.Torso.ParticleEmitter.Enabled = true
end

if you just put these lines of code the game will only check once and doesnt run more than once

use a while true do or run service

while wait() do
	--if health is below 50
--checks every 1/30 of a second
end

run service

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	--checks every single frame if health is below 50
end)

4 comments

0

if you have any questions on how runService works ask me

0

RBXSignals are basically checks that runs every frame which is what ArsonMcFlames did

0

Nah, don't use this way. Just use .HealthChanged() to detect when the humanoid's health changes. I'm pretty sure the way you're suggesting would bog down the game if many scripts of this type were added into a single game.

0

thanks to both of you!

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