Trying to make a increase and drecrease system (my first module system)

Hello! How can i make it works? I’m trying to do this for some humanoid attributes (sanity, stamina, something like that) to when i call this function i can just put the value to decrease ou increase and the attribute inside the parenthesis.
Code to be Reviwed:

local Increase = {}

function Increase.Increase(value: number, stat)
	stat += value	
	print("Increased")
end

function Increase.Decrease(value: number,stat)
	stat -= value
	print("Decreased")
end

return Increase

If the stat is a variable you can leave it like that if it is a number value you need to do: stat.Value += value and stat.Value -= value

The stat is a attribute (setattribute)

Then you need to use SetAttribute and GetAttribute.
Maybe make the function to take the humanoid as an argument and then humanoid:SetAttribute(stat, humanoid:GetAttribute(stat) + value)

1 Like

Thanks, i can call a variable that is not on the module script and just on the script or local script i’m calling it? cuz i dont know if is possible call the humanoid by a module script.

NVMD! IT WORKED! The only problem is i did not make the value updates but it works perfectly!! <3333

local API = {}
API.__index = API

function API.new(humanoid)
    local self = setmetatable({
         _humanoid = humanoid,
         _stamina = 0,
         _sanity = 0,
    },API)

    return self
end

function API:AddStamina(stamina)
    self._stamina += stamina
end

usage for this:

local module = require(modulepath)
local new_humanoid = module.new(humanoid)
new_humanoid:AddStamina(10)

This is OOP version

1 Like

You need to pass the humanoid to the function as a third pramater instead of just passing the stat and the value.

Finished code:

local Module = {}

function Module.Increase(value: number, stat: string, humanoid: humanoid)
	humanoid:SetAttribute(humanoid:GetAttribute(stat)+value)
	print("Increased")
end

function Module.Decrease(value: number, stat: string, humanoid: humanoid)
	humanoid:SetAttribute(humanoid:GetAttribute(stat)-value)
	print("Decreased")
end

return Increase

I don’t mean to insult your efforts, but this is an extremely redundant use of OOP if all you need to do is add or subtract a value

1 Like