Funcs

Put a single function in a file of its own and require it wherever you need it.

A func file returns one function. Its type is registered in the folder's Types under the func's name, and plain locals above it keep any state between calls.

A func

A server func that hands out a new id on every call:

Wallet/NextId.shot

types script.Parent.Types

rootdoc {
    Returns a new id every call: "S 1", "S 2", ...
}

local counter = 0

server func NextId(prefix : string) : string
    counter += 1
    return prefix .. " " .. tostring(counter)
end

Using it:

local NextId = require(@shot.Wallet.NextId)

local id = NextId("S")

Rules

  • func Name(params) : Return ... end, with server or client in front for a one-sided func.
  • The file returns the function. Its type, (prefix : string) -> string, is registered as NextId in the folder's Types, so other files can use NextId as a type.
  • Type parameters are fine: func First<T>(list : {T}) : T?
  • State goes in locals above it (counter above). Code below it may call it, for example to answer a remote:
server func GetId() : string
    ...
end

script.Parent.GetIdRemote.OnServerInvoke = function()
    return GetId()
end
  • A func has no members, so it has no doc { } inside. A doc { } or rootdoc { } above it documents the whole file (see Doc comments).

For a func with one version on the client and another on the server under one name, use a diffsource func (Server and client, DIFFSOURCE FUNC).