Module function doesn't update module variables

Hello, I have been trying to fix this problem for a while now but still haven’t seem to find the solution.
I am trying to change a variable in a module with a function that is also in the same module, but for some reason, it won’t update for the other functions in that module.

Module’s Variables :

function CharacterStats.new()
	local self = setmetatable({}, CharacterStats)

	self.Weapon = {
		Name = "Wooden Sword";
		Type = "Sword";
	}

	self.ArmorSet = {
		Head = nil;
		Torso = nil;
		Legs = nil;
	}

	self.Stat = {
		["Physical Boost"] = 10;
	}
	
	self.Colors = {
		None = Color3.fromRGB(255, 255, 255);
		Physical = Color3.fromRGB(255, 106, 92);
		Magic =  Color3.fromRGB(98, 192, 255);
		Dexterity =  Color3.fromRGB(255, 193, 117);
		Holy =  Color3.fromRGB(255, 238, 0);
		Choatic =  Color3.fromRGB(255, 0, 111);
	}

	return self
end

Module’s Change Function :

function CharacterStats:GiveStats()
	
	self.Stat = {
		["Physical Boost"] = 100;
	}
	
end

If I try to print self.Stat in any other function it still prints out as [“Physical Boost”] = 10 instead of 100.
How would I go about making this work?

It appears the problem isn’t in the provided code.

It’s very important your class has appropriate set metatables, and __index is pointing to the class.

Shortened module
local CharacterStats = {}
CharacterStats.__index = CharacterStats

function CharacterStats.new()
	local self = setmetatable({}, CharacterStats)
	
	self.Stat = {
		["Physical Boost"] = 10;
	}
	
	-- . . .
	
	return self
end

function CharacterStats:GiveStats()
	self.Stat = {
		["Physical Boost"] = 100;
	}
end

return CharacterStats

Script:

local CharacterStats = require(path_to_module)

local NewStats = CharacterStats.new() -- construct a new object
print(NewStats.Stat["Physical Boost"]) --> 10
NewStats:GiveStats() -- pass the object as self to GiveStats
print(NewStats.Stat["Physical Boost"]) --> 100

Sorry I didn’t put that in part of the script, but I do have __index.
And I did it in the exact way you have done it but hasn’t worked for me.

I found the solution. (Don’t know if this is the best way to do it tho)
I just made a separate module for the functions and left the Stats in the same module.

A single class is generally not meant to be split into separate modules, but it can be. I can’t really know how you did it, but if it works for you and you think it fits your code, then it’s probably fine.

There must be some hidden detail. The example I sent does set “Physical Boost” to 100, and can be read from other methods. If you’d like to still try to fix the original, you can just reply here with more details from your code (like how you called the methods) or send me a private message.

Update.

Solved in private messages.

Thanks for taking the time to reply to me, but yeah I would probably keep it like this. But I sure still would like to know why it didn’t work. (Btw I can’t private message you)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.