What does the pipeline character do in functions (LUA)

local function get_player(name: string): Player | nil
    name = name:lower()
    --...
end

I have this dummy function (literally just from a piece of code I saw).

What does the (name: string): Player | nil do? I know that ‘name’ is the parameter, ‘string’ is the parameter accepting type, but what is the colon ( : ) after string) for? And how does the pipeline character work?

Thanks ahead.

That’s just type checking
Since roblox is not statically typed language it does something similar to TS to have types.

its primarily for aesthetics

3 Likes

it just returns what the function will return

function get_player(name: string?): Player?
    if game:GetService("Players"):FindFirstChild(string.lower(name) then
      return game:GetService("Players"):FindFirstChild(name) -- will return player.
    end
end
1 Like

so when I’m typing out the function, I would see “Player?” on a ribbon near my cursor?

Yes. Assuming the functions finds the player and returns it.

1 Like

That’s nice! Thx for the link! It’s caused I never knew there was a use for the pipeline in LUA

Oh so it’s not just a description or custom text that works like reminders for devs?

1 Like

It’s typechecking, like descriptions.

2 Likes

This means that it expects the name to be a string
And it will either return a Player or nil

Player | nil is the same as Player?

1 Like

thank you, but a little more detailed, does the ( | ) mean “or” in type checking?

thank you! =) characters characters

Yes, the type returned by the function will be either a Player or nil.

To enable type checking on scripts put --!strict at the very top, line #1

1 Like

I’m not going to mark a solution to a specific user, because all of ya’ll made big help. It’s not fair to give just one user the solution mark.

2 Likes

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