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

else not working when checking for variable existence. any help?

 local tool
    if not tool then
        print"e"
       else
    print"c"
end

it won't print "c" whatever I change 'tool' to, any help appreciated

1 answer

I assume you're trying to assign a variable named tool to whatever value the ternary statement evaluates to? If this was the exact code you used, then you missed an equal '=' operator during the assignment.

local tool = if not tool then print("e") else print("c") 

Keep in mind, however, that this only works if the tool is nil or false. Also, the variable named tool will be reassigned to whatever print() returns.

3 comments

0

I also included parenthesis on the print function just for clarity. You can omit them for strings if you want

0

it's more like checking if a block exists in a game. if game.workspace.part then <- guessing this checks if a part exists

0

if you want to check if a block exists in workspace, you can use Part:IsDescendantOf(workspace)

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