Whats the point of having a question mark in arguments?

So I’m wondering what is the purpose of having a question mark inside an argument?

-------------------------------------------------------------------------------
function DoSomething(P: Instance)
end
-------------------------------------------------------------------------------
function DoSomething(P: Instance?) -- Question mark?
end
-------------------------------------------------------------------------------

Appearently its supposed to be an “optional” thing, but what does it do to the code?

The question mark means variable or nil, in this case that variable can be an Instance or nil.

Does it affect the code?, no, the types in the script don’t affect the way it works, it just makes it easier to read and edit.

It’s common in Lua for function arguments or other values to store either a value of a given type or nil; this is represented as a union (number | nil), but can be specified using ? as a shorthand syntax (number?).
Syntax - Luau

I’m pretty sure without the Question mark its the same exact same thing?

Yes, it is the same, but it would possibly show a type error
image

image

image

2 Likes

Strange, I don’t get that error :confused:

What is the script?, appears depending on the use of the variable (as above, a nil variable has no Parent).

Just a Standard Script:

function A(p: Instance)
	p.Parent = workspace
	--print(p)
end
A()

function B(p: Instance?)
	p.Parent = workspace
--print(p)
end

B()

I get no red underline, or errors, odd

(Testing it again, got errors)

It should appear
image


Do you have this beta feature activated?
image

You can also put --!strict in the first line.

Nope, i dont use Beta Features, i usually just have them off by default

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.