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

How do I make something happen when a humanoid's health is below 50?

Asked by 5 years ago

I'm trying to make this script so that when an NPC's health goes below 50 it'll put a shield up, but nothing happens. Filtering Enabled is on.

name="Humanoid"


while true do
    wait(1)
    if script.Parent.Humanoid.Health<50 then
        script.Parent.Torso.Proj.Beam.Enabled = true
        script.Parent.Torso.Shield.Script.Disabled = false
        script.Parent.Torso.Shield.Transparency = .5
        script.Parent.Torso.Shield.CanCollide = true
    else
        script.Parent.Torso.Proj.Beam.Enabled = false
        script.Parent.Torso.Shield.Script.Disabled = true
        script.Parent.Torso.Shield.Transparency = 1
        script.Parent.Torso.Shield.CanCollide = false
    end
end

Thanks if you can help me!

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Don't use a while loop to do the job a Changed event should do. Also, place this script in StarterPlayerScripts, as the script won't run again if the character respawns and the script was placed in StarterCharacterScripts.

local plr = script.Parent.Parent
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char.HumanoidRootPart

char.Humanoid.HealthChanged:Connect(function(new)
    if new <= 50 then
        hrp.Proj.Beam.Enabled = true
        hrp.Shield.Script.Disabled = false
        hrp.Shield.Transparency = .5
        hrp.Shield.CanCollide = true
    else
        hrp.Proj.Beam.Enabled = false
        hrp.Shield.Script.Disabled = true
        hrp.Shield.Transparency = 1
        hrp.Shield.CanCollide = false
    end
end)

I also put the objects in the humanoid root part as all rigs have one.

0
Is there a way to make it so the shield doesn't have the humanoid take damage? CaptainAlien132 225 — 5y
0
Check if the shield is present. If it is, deal less damage/don't deal damage at all. User#19524 175 — 5y
0
Alright CaptainAlien132 225 — 5y
Ad

Answer this question