How would I get about using a ModuleScript to save a character's data?

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
1 Like

You need to initialize the Players and NPCs tables outside of any function at the top level of the module so that they are shared across all scripts

local rep_ser = game:GetService("ReplicatedStorage")
local types = require(rep_ser.Modules.Types)

local Stats = {}

--data tablles
Stats.Players = {}
Stats.NPCs = {}
--or
local Stats = {
	Players = {},
	NPCs = {}
}
1 Like

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

plr.CharacterAdded:Connect(function(chr)
	chr.Parent = workspace.Characters
	chr.PrimaryPart = chr:FindFirstChild("HumanoidRootPart")

	stat_handler.AddEntity(plr)
    stat_handler.PrintPlayerList()
		
	chr:AddTag("Hittable")
end)

This does print it out correctly

But, now, if the module is accessed by the player in a localscript, the output is just an empty table
image

1 Like

Yes you should use remotefunctions for this

1 Like

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

1 Like