Home > other >  If Statements in Lua
If Statements in Lua

Time:12-26

In all other programming languages I've encountered, if statements always require a boolean to work, however in Lua the following code does not contain any errors. What is being checked in this if statement if both true and false statements can't be made? How can you only check "if (variable) then"? I am new to programming and currently working with Roblox Studio, any help would be very appreciated.

function onTouched(Obj)
        local h = Obj.Parent:FindFirstChild("Humanoid")
        if h then
            h.Health = 0
        end
    end

    script.Parent.Touched:Connect(onTouched)

CodePudding user response:

if h == nil (null)

So if it couldn't find a humanoid in the object that touched the script's parent, it will be false (null), otherwise true (not null).

So if [ObjectName] then equals to if [ObjectName] != null then
*Only valid for objects (non primitive values)

It's like that in script languages.

CodePudding user response:

Most languages have their own rules for how values are interpreted in if statements.

In Lua, false and nil are treated as false. All other values are treated as true.

  • Related