What does :word do in a function?

Hello! I browse the DevForum a lot, and when I look in #resources category, I often see functions written like this:

-- Example Instance.new function where the 2nd parameter can be used, but it has a concept!
local function CreateObject(ClassName: Name, Parent: Parent, TableForProperties: Table)
   local Object = Instance.new(ClassName)
   for Property, Value in pairs(TableForProperties) do
      Object[Property] = Value
   end
   Object.Parent = Parent
   return Object
end

local Object = CreateObject("Part", workspace, {Material = Enum.Material.Wood, Transparency = 0.5})

Why do they have

ClassName: Name

for example? What is it used for? If you could answer for me, please let me know. Thanks! WE

2 Likes

It’s a type annotation. You can read more about it here.

So basically, its just a comment for functions?

function lol(sasuke: string, naruto: string --[[type]]): string -- returns
  return ('%s vs %s'):format(sasuke, naruto)
end

print(lol('sa sus ke', 'naruto us'))

Im still confused on what they do.

just messages, shows to the function user what to input and what it returns

Ok. So I was correct up here:::

I recommend reading about typescript:

It can be a comment for functions if you want it to be, but using gradual typing, you can use it to define new types to make your code much cleaner.

You can read even more about it from:

2 Likes