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.
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
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.
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)