Check for Nil Value isn't actually noticing the "nil"

Hello! I have this script checking if a value is nil and not firing if it is, but the scipt continued despite the value being nil.

if ArmorType == nil then ArmorType = "Default" end
Shot = true
print("Attempting The Final Damage With Armor: "..ArmorType)
print("Team Result: "..tostring(TeamResult))
print("Hit Part: "..HitPart.Name)	
warn("Damage Warning: "..tostring(SettingsService.Settings.Damage[ArmorType][HitPart.Name]))										
if SettingsService.Settings.Damage[ArmorType][HitPart.Name] ~= nil and TeamResult ~= "Friendly" or TeamResult == false or TeamResult == nil then
--The above statement should be preventing below from running if it's nil, but the script continues anyway.
HitModel:FindFirstChild("Humanoid").Health -= SettingsService.Settings.Damage[ArmorType][HitPart.Name]
print("Shot N' Damaged")
end

This was the output:

I’m not sure why this is happening, is this me overlooking the error?

try this:

if ArmorType == nil then 
    ArmorType = "Default" 
end

Shot = true

print("Attempting The Final Damage With Armor: "..ArmorType)
print("Team Result: "..tostring(TeamResult))
print("Hit Part: "..HitPart.Name)	
warn("Damage Warning: "..tostring(SettingsService.Settings.Damage[ArmorType][HitPart.Name]))	
									
if SettingsService.Settings.Damage[ArmorType][HitPart.Name] ~= nil and (TeamResult ~= "Friendly" or TeamResult == false or TeamResult == nil) then
   HitModel:FindFirstChild("Humanoid").Health -= SettingsService.Settings.Damage[ArmorType][HitPart.Name]
   print("Shot N' Damaged")
end
3 Likes

You could try:

local ArmorType = nil
if not ArmorType then
    ArmorType = "Default"
end
1 Like

Wow, that worked! If you don’t mind, could you explain how that fixed it?

Earlier in the script something happens to the Armor Value, thank you though!

No problem! You could do:

if not Variable then
--This will check if the variable doesn't exists, aka if it's nil
end

Basically, I added parenthesis around

TeamResult ~= "Friendly" or TeamResult == false or TeamResult == nil

Because it would wrap it together to check if only one value would return true, the former code would check if one value was true and if it wasn’t then it would check the next.

I’m not good at explaining but think of it like a math equation with parenthesis

1 Like