I am new to Roblox scripting (Lua apparently) and was fiddling with a function that is called when one Part (dubbed the "Sender") is touched by another Part, which is actually a tool with a Part called "Handle" inside of it, and a third Part nested inside that. I have also added an IntValue to the latter to be read upon touching.

The object tree looks like this:

__Apple (Tool)
  L_Handle (Part)
    L_Value (IntValue)
    L_Apple (Part)
    L_Handle-to-Part Strong Joint

The function is called alright, and the part with the name "Handle" is correctly identified, but I cannot for the world get a hold of the IntValue using the following code:

function onTouch(hit)   
    print("Sender touched by "..hit.Name)
    local val = hit:FindFirstChild("IntValue", true)
    if val == not nil then
        print("Found IntValue: "..val.Name)
        if val.Name == "Cash" then
            --Get Money For Apple
            print("Players: "..game.Players.LocalPlayer.Name)
            game.Players.LocalPlayer.leaderstats.Money.value = game.Players.LocalPlayer.leaderstats.Money.value + val.value 
        print("IntValue Not Found!")

In other words: It keeps on logging "IntValue Not Found"! I have changed the name of the IntValue to "Value" and back to "IntValue" to no avail. I have been adding clones of it to several different layers of the composite object, but no results.

Any help is greatly appreciated!

BTW The object is a Tool, because the player is supposed to be able to pick it up and place it on the "Sender".


I think your misunderstanding is based around what val == not nil means. The expression not nil evaluates to true, so the if-statement is really saying—

if val == true then

That only evaluates to true when val is exactly the Boolean value true. For example, 'true' == true is false.

As your answer indicates you've discovered, when you change the predicate to just—

if val then

—you will find that any values other than false and nil are "truthy" and will pass the if-statement. This is explained by the Lua manual in §3.3.4:

The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).

If you find yourself using Lua a lot, I highly recommend reading through the rest of the Reference Manual too. It's exceptionally clearly written.

|improve this answer|||||

I figured out the answer myself, although I do not fully understand why, it does stem from my inexperience with Lua. I was trying to transfer my habits from other programming languages.

This line:

if val == not nil then

Needs to be changed to:

if val then

And now it works!

Apparently what is returned by the FindFirstChild() method is a Boolean?

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.