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

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

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

✓ Accepted answer

###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.

3 comments

0

Is there a way to make it so the shield doesn't have the humanoid take damage?

0

Check if the shield is present. If it is, deal less damage/don't deal damage at all.

0

Alright

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