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

Why isn't script properly Checking Humanoid?

Purpose Of Script: Make the values true when Health is 50 and bellow , then make it false if above 50

Problem: It does not change the values, there are no errors in the output and the only things that are printed are-Checking-Good Hp-false-false, I have used a sword in my place and when it's health is clearly below 50 or even 0 It still says Good Hp, I do not know what's the problem, I have tried things like changing the variables to waitforchild,findfirstchild and regular format(script.parent.[NAME])

Script Location: Its located in the NPC there is a humanoid, and is named such, it is a regular script

Script

local humanoid = script.Parent:FindFirstChild('Humanoid')
local Health = humanoid.Health
local MaxHealth = humanoid.MaxHealth


function Check2()	
local Boss = script.Parent
local FieldDefense = script.Parent:WaitForChild('FieldDefense')
local Healing = script.Parent:WaitForChild('Healing')		
	
if Health > 50 then
print('Good Hp')
FieldDefense.Value = false	
Healing.Value = false
print(Healing.Value)
print(FieldDefense.Value)
end	
end

function Check()

local Boss = script.Parent
local FieldDefense = script.Parent:WaitForChild('FieldDefense')
local Healing = script.Parent:WaitForChild('Healing')
	
if Health <= 50 then	
print('Low Hp')
FieldDefense.Value = true
Healing.Value = true
print(Healing.Value)
print(FieldDefense.Value)
end
end

while wait(.1) do
print("Checking")
Check()	
Check2()
	
	
end

2 comments

1

Your question structure and information pleases me. Have a cookie.

0

lol, I'll take some help instead, and also a vote up since I cannot chat i the SH Chat ;-;

2 answers

✓ Accepted answer

Health never updates.
Last updated: Never.

In your script, Health is always Humanoid.Health according to the value it was when the script had run. I'd recommend using events such as Humanoid.HealthChanged to drive everything, and update your Health variable before running your checks.

6 comments

0

humanoid.HealthChanged:connect(function(health) Health = Health end)?

0

That would do it. I'd actually advise getting rid of the while loop and replacing it with an event connection like that.

0

It still no workeh ;(

0

Well you can't set Health to Health. Set it to health instead.

0

so..humanoid.HealthChanged:connect(function(health) Health = health end)

0

Yes.

I recommend to REMOVE the health & MaxHealth

health = humanoid.Health

It's because if you do that, it won't work that way!

Here's a code that will work!

local humanoid = script.Parent:FindFirstChild('Humanoid')

function Check2()  
local Boss = script.Parent
local FieldDefense = script.Parent:WaitForChild('FieldDefense')
local Healing = script.Parent:WaitForChild('Healing')      
if humanoid.Health > 50 then
print('Good Hp')
FieldDefense.Value = false 
Healing.Value = false
print(Healing.Value)
print(FieldDefense.Value)
end
end
function Check()
local Boss = script.Parent
local FieldDefense = script.Parent:WaitForChild('FieldDefense')
local Healing = script.Parent:WaitForChild('Healing')
if humanoid.Health <= 50 then   
print('Low Hp')
FieldDefense.Value = true
Healing.Value = true
print(Healing.Value)
print(FieldDefense.Value)
end
end
while wait(.1) do
print("Checking")
Check()
Check2()
end

If it doesn't work, please PM me.

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