Alternatives to embedded if statements?

Hey, I am currently doing a lot of sanity checks for my script. As I have to be 100% sure that no errors will occur. So, the way I am doing it currently is by using embedded if statements. But it’s quite tedious and I end up with (literally) hundreds of ends all over the place.
An example would be like the one below;
I want to make sure that the character is a real player and has a humanoid, then I want to make sure (if it’s a real player) that the player is not dead.

if character:FindFirstChild("Humanoid") then
        if character.Humanoid.Health ~= 0 then

        end
end

You see where I am going? It’s really bad. So I was wondering if there are any other ways. And yes:

if character:FindFirstChild("Humanoid")  and character.Humanoid.Health ~= 0 then

end

Does not work, as it would error if the first statement is false. Thanks, I am looking forward to feedback!

It will not error.
Lua doesn’t check the second statement given the first one is false/nil
Why would it anyways?

  > if 1~=1 and nil+3 then end print('OK')
  OK
1 Like

Oh really? I’d have to check that right away.

But regarding, say,
if workspace:FindFirstChild("Something") and workspace.Something.Value == 10 then

And the “Something” variable is non existent, it will not error?

Edit: It works. I must’ve done something wrong testing it out earlier.

Nevertheless, STILL OPEN FOR IDEAS!

There is basically no way to do the same thing without using if statements / unary operators. Unless: (Since you are going for no error)

local a = workspace.Value
pcall(function() a.Value += 1 end)

Which is just lazy.

1 Like