Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago

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.

Ad
Log in to vote
0
Answered by 3 years ago

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)
0
if you have any questions on how runService works ask me proROBLOXkiller5 112 — 3y
0
RBXSignals are basically checks that runs every frame which is what ArsonMcFlames did proROBLOXkiller5 112 — 3y
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. TheB4dComputer 100 — 3y
0
thanks to both of you! surviarlTheJefkillre 55 — 3y

Answer this question