What do " ? " (s) signify in Lua?

Recently I saw " ? " being used in some scripts (to be honest module scripts).
The link to the module script (by Bricey):
Mouse Module - Roblox
and you will see the ? being used in certain lines of code.
Just a small shot:

I just wanted to be clarified about the ? and if they really signify anything in lua and if they do when to use them.

Incase someone does not wanna click on the link here is the code in the module:

local UserInputService = game:GetService(“UserInputService”)

local MAX_DISTANCE : number = 500

local cam : Camera = workspace.CurrentCamera

local Mouse = {}

local leftClick : BindableEvent = Instance.new(“BindableEvent”)
local rightClick : BindableEvent = Instance.new(“BindableEvent”)

–References to bindable events that act similarly to the original mouse
Mouse.LeftClick = leftClick.Event
Mouse.RightClick = rightClick.Event

function Mouse.GetUnitRay() : Ray
–Get position of mouse on screen
local x : number, y : number = UserInputService:GetMouseLocation().X, UserInputService:GetMouseLocation().Y

–Convert that to a unit ray pointing towards the camera CFrame
local ray : Ray = cam:ViewportPointToRay(x, y)

return ray
end

function Mouse.Raycast(rayParams : RaycastParams?) : (RaycastResult?, Ray)
local unitRay : Ray = Mouse.GetUnitRay()

–Raycast from the origin to max dist
local result : RaycastResult? = workspace:Raycast(unitRay.Origin, unitRay.Direction * MAX_DISTANCE, rayParams)

–Give them the RaycastResult and Unit Ray, if they need it
return result, unitRay
end

function Mouse.GetPosition(rayParams : RaycastParams?) : Vector3
–Get the current Mouse Position, either as a raycastResult or Ray
local raycast : RaycastResult?, unitRay : Ray = Mouse.Raycast(rayParams)

--Return the appropriate value 

if raycast then
–The raycast was successful, so we can just get the position
return raycast.Position
elseif unitRay then
–Raycast was not successful, we need to manually calculate the intended position
return unitRay.Origin + unitRay.Direction * MAX_DISTANCE
end
end

function Mouse.GetTarget(rayParams : RaycastParams?) : Instance?
–Call the raycast, forwarding the rayParams object
local raycast : RaycastResult, unitRay : Ray = Mouse.Raycast(rayParams)

–Give them target if it exists
return raycast and raycast.Instance
end

function Mouse._OnInput(obj : InputObject)
if obj.UserInputType == Enum.UserInputType.MouseButton1 then
leftClick:Fire(obj.UserInputState)
elseif obj.UserInputType == Enum.UserInputType.MouseButton2 then
rightClick:Fire(obj.UserInputState)
end
end

UserInputService.InputBegan:Connect(Mouse._OnInput)
UserInputService.InputEnded:Connect(Mouse._OnInput)

return Mouse

1 Like

In typed luau, that means it can be nil, meaning it’s optional. Learn more here:

Options
It’s pretty commonplace to have optional data, so there is extra syntax for describing a union between a type and nil . Just put a ? on the end. Function arguments that can be nil are understood to be optional.

2 Likes

Hey thanks for the solution.
I had another question which I thought asking you would be better as you are using the terms in your reply.
“Luau”,whats the difference between Lua and Luau and when should I use Luau or lua.

1 Like

lua is a programming language. Luau is a sort of extension of lua made by roblox. You use luau when coding in roblox.

2 Likes

Thanks for clarifying that to me, so that I don’t use Luau in a case outside of roblox.

1 Like

Since this is a common misconception, I’ll add on to it.
Lua can be tied into any existing software and you can create an API in Lua to be able to do stuff with it. Things like creating a new part or detecting when someone clicks a button are just part of Roblox’s API, and it is perfectly normal to have things like this in regular Lua. We used to use regular Lua 1.5.4 actually.

The part of the language that is Luau is the typed language bits that Steven mentioned which allow you to specify that a variable can only be a number, or that variable can only be a string. There are a number of advantages to this especially when debugging, and as strongly typed luau features become more popular we will likely see a boost to debugging capabilities. Luau also adds assignment operators such a x+=5, and a bunch of performance fixes either not native to Lua or specific to Roblox.

Unless you are using the strongly typed portion of the language, almost all of what you learn of Luau will be doable in Lua.

1 Like