Is there a way to use a value if available, or a default, in a single line?

BY which I mean, for example, a StringValue.Value. (EDIT - I meant NumberValue)

local lootBox = script.Parent
local quantity = lootBox:FindFirstChild("Quantity").Value or 1

This doesn’t work, because if Quantity is nil, I can’t read the Value. But is there a way to initialise quantity using that value or a default in one line?

EDITED FOR CLARITY
Is there a way to put the following on a single line?

local quantity
if lootBox:FindFirstChild("Quantity") then
  quantity = lootBox.Quantity.Value
else
  quantity = 1
end

what is lootbox supposed to be a stringValue?

if that is your question no you can’t use numbers in stringvalues if you want a string and numbers
you must have a numbervalue and a stringvalue

It’s meant to be a NumberValue. That was a mistake I made when typing the post.

That’s not the question though. How do I initialise quantity to the Value of Quantity if Quantity is not nil, or 1?

if your having problems with it returning a non integer value you can do a check in the code your writing like:

if lootbox.intvalue.value ~= nil then
run the script you are trying to run
end

does this help you or is the problem something else?

It’s not the value that will be nil. It’s (in your example) intvalue

Is this what you mean?

local quantity = lootBox:FindFirstChild("Quantity") and lootBox.Quantity.Value or 1
2 Likes

you can remove white spaces however it is not possible to put blocks of code into one line

Wow, that is such a clean and crisp way of writing code.

I was thinking about using semi colons, then remembered we’re using Roblox.

Have a heart from me!