Not sure what the : does in a module script and how to correctly require it

I’ve recently just followed the dev forum tutorial on making a data store saving script, (which I completely understand how it works) but just unsure about one part in the function.

function PlayerStatManager:ChangeStat(player, statName, value)

I know this would be the function to call to change the values, but I’m not entirely sure how I would call it using a separate script. The values currently are Money and Experience.

If you’re asking how to require the module to transfer information to the script you would do something like this:

local ModuleScript = require([location].ModuleScript)

Change the location to something like script.Parent.Parent – that’s what I mean by location

I know how to require a module script, I’m asking how to change a value after requiring the module script. The code after that simply just increases the value in a table.

--Module Script
local module = {}

functions module:ChangeStat(player, statName, value)
    --Your code that changes the value
end

return module



--Server Script

local module = require(Path.to.module)

module:ChangeStat(player, statName, value)  --Just call the method

I did think that, what is the difference between using a ‘.’ and a ‘:’?

a:b() is basically syntactic sugar for a.b(a)

Using : passes the table that the function was a part of as the first argument. This argument is automatically set up as self if the function is defined using a : as well. So, in that case, self would be module.

More info here: Programming in Lua : 16

The colon is only a syntactic facility, although a convenient one; there is nothing really new here.

2 Likes