ModuleScript assistance

I am having difficulty with understanding ModuleScripts that use tables and If possible, I’d like an example of an NPC’s Stats being inside a table in a ModuleScript and how I would go about applying them to instance values. On another note, I am wondering how I would go about spawning in NPCs to a folder in workspace with a ModuleScript required from one Script in my game. Any help is appreciated!
My discord: Sinner#3829

Server Script, LocalScript, or another ModuleScript:

local module = require(script.ModuleScript) -- get the module script

The Actual ModuleScript:

local module = {}

-- values in the "module" table, which the script returns
module.Health = 100 -- set the default value of a "health" value
module.Speed = 16 -- set the default value of a "speed" value etc..
module.Name = "NPC" -- the npc's name
module.Level = 15 -- the npc's level
-- any index and value can be set the table of the modulescript

-- functions for which the table/modulescript can do
function module:SetHealth(value) -- uses a colon because it's a method
   self.Health = value -- set the npc''s health
end

function module:GetHealth() -- a function that gets the health in the table
   return self.Health -- returns the "health" value from the table
end

-- or you can define methods a different way

module.SetHealth = function(self, value) -- auto-defines "self"
   self.Health = value -- set the value
end

module.GetHealth = function(self)
   return self.Health -- return the value of health
end

-- !!!! NOTE: Do not define the same function twice! I just use them as examples

-- the keyword "self" is auto-defined to the module because the functions are methods (uses colons)
-- or in short, "self = module"

-- the keyword "self" isn't auto-defined in normal functions because they aren't methods

--[[
    example of functions that doesn't have an auto-defined "self"

    function module.new()
        print(self) <- prints nil
    end

    module.new = function()
        print(self) < prints nil
    end
--]]
return module -- returns the table which the script can use

Back to the script

local module = require(script.ModuleScript) -- get the module script
-- you can now use the methods and indexes that were defined in the modulescript

print(module.Health) -- or you can do "print(module:GetHealth())"
-- prints "100"

module:SetHealth(50) -- calls the function that was defined in the ModuleScript
print(module.Health) -- prints 50
-- To apply the value in BaseValues
local module = require(script.ModuleScript)

local value = Instance.new("IntValue") -- creates a new integer value
value.Name = "Health" -- set the Instance's name
value.Value = module.Health -- sets the IntValue's value to the "Health" index
value.Parent = workspace -- sets the parent of the IntValue to workspace

To make sure that the value is a positive value you can do

value.Value = math.abs(module.Health) -- gets the absolute value of the "Health" index (always positive)

I’m not that good at giving explanations

1 Like