Hi! I am currently testing the new Roblox feature which is type-checking.
I am making a module with useful functions to be used by other scripts, but I have had a problem, and that is that I can’t specify a function when the module variable has been created.
This is my code:
-- MODULE
local UtilsFunctions: Typedef = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
return ""
end
function UtilsFunctions.CreatePlayerLeaderstats(playtime: number): ()
UtilsFunctions.GetPlaytimeLevel(1234) -- I need to use this function so this function can work.
end
-- EXPORTS
export type Typedef = {
GetPlaytimeLevel: ((playtime: number) -> string),
CreatePlayerLeaderstats: ((playtime: number) -> ())
}
-- RETURNS
return UtilsFunctions
Luau has a built-in typeof function, try that maybe?
--!strict
-- MODULE
local UtilsFunctions: Typedef = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
return ""
end
function UtilsFunctions.CreatePlayerLeaderstats(playtime: number): ()
UtilsFunctions.GetPlaytimeLevel(1234) -- I need to use this function so this function can work.
end
-- EXPORTS
export type Typedef = typeof(UtilsFunctions)
-- RETURNS
return UtilsFunctions
do end makes a new temporary scope that is active until it reaches the end from the do statement, since using = {} is the one causing issue, I’ve went ahead and made a new scope and defined the variable there (which also in return give you working typechecking under the cost of “weird” code structure)
It’s like making a function and then calling it instantly, except this one just makes a scope that runs once following the flow of the code instead of being able to be called from any line