I’m trying to make it so that I have a ModuleScript that stores every player’s and every NPC’s data (while this wasn’t my preferred option, I’m just trying to figure out how to do it first, then maybe I’ll have more ModuleScripts for each entity), but I can’t figure out how to change the values in any entity without the table resetting entirely every time the script is required? It works, until you have to require it from another script, then all the data is lost and has to be readded, this is my script for reference:
local rep_ser = game:GetService("ReplicatedStorage")
local types = require(rep_ser.Modules.Types)
local Stats = {}
function isPlayer(target: string)
return table.find(target, Stats.Players)
end
function Stats.AddEntity(plr: Player)
local set_stats: types.CharacterInfo =
{
Player = plr,
Model = plr.Character,
Moveset = "TestCharacter",
Endurance = 5,
HasRevived = false,
CanAct = true,
Stun = "None",
DamageMultiplier = 1,
StunMultipler = 1,
Effects = {},
Cooldowns = {}
}
if plr then
Stats.Players[plr.Name] = set_stats
end
end
function Stats.GetEntity(target: string)
return Stats.Players[target]
end
function Stats.RemoveEntity(target: string)
if isPlayer(target) then
Stats.Players[target] = nil
return
end
Stats.NPCs[target] = nil
end
function Stats.ChangeStat(target: string, stat_name: string, stat_value)
if isPlayer(target) then
Stats.Players[target][stat_name] = stat_value
return
end
Stats.NPCs[target][stat_name] = stat_value
end
return Stats
Oops, I forgot to add that back in after a few attempts of trying to fix it
Still doesn’t change much, though
Supposedly added the player to the list, and it does work when printing it out from the same script that added it, but when accessed to by another script, the table is initialized again, and it resets
you can just make a modulescript that handles saving and loading stuff then from a serverscript,
you call it when needed like when the player joins or leaves you can listen to characteradded to grab what you want from the humanoid like health or whatever and store it in a table then when saving you send that table to your datastore function just make sure you do everything on the server since the client cant access datastore and try to keep the module clean so its reusable like getdata save getdefault and stuff like that