Garbage collection deleting my table

i have a module script that holds all the players states, I put it into its own table and throw in values based on the character they choose, the server calls the module script and it makes a table and the table is alive for a bit, and then it just poofs out of existence
–yes they’re are stats within the kaiser table, i think its because nothing else has access to the table, but how do i stop this

local players = game:GetService("Players")

local stateHandler = {}

local characterStats = {
	["Kaiser"] = {
		["baseSpeed"] = 12,
		["characterName"] = "Kaiser",
		["chargedShot"] = 0,
		["jumpHeight"] = 7.2,
		["maxStam"] = 100,
		["shotPower"] = 320,
		["stam"] = 100,
		["stun"] = 0,
		["tackleCooldown"] = 0.5,
		["tackleDuration"] = 1,
		["topSpeed"] = 28,
		["CanPossess"] = false,
		["CanRun"] = true,
		["CanShoot"] = true,
		["Possessor"] = false,
		["isTackling"] = false,
	}
}

function stateHandler.addToList( player : string , characterName : string )
	if not characterStats[characterName] then
		warn("Did not find valid character")
		return
	end
	
	if players:FindFirstChild(player) then
		local character = characterStats[characterName]
		if not stateHandler[player] then
			stateHandler[player] = {}
			local newPlayerTable = stateHandler[player]
			
			for i, stat in pairs(character) do
				newPlayerTable[i] = stat
			end 
			
		end
		
	else
		warn("passed player was not a valid player "..player.Name)
	end
	print(stateHandler)
end

function stateHandler.removeFromList(player : Player)
	
	if stateHandler[player] then
		print("Removing"..player)
		table.remove(stateHandler, player)
	end
	
end

function stateHandler.getStat(player : Player, desiredStat : string)
	print(player.Name)
	print(stateHandler)
	print(stateHandler[player])
	if stateHandler[player] then
		for i, stat in pairs(stateHandler[player.Name]) do
			if i == desiredStat then
				print('found your stat')
				return stat
			else
				print(i)
			end
		end
	else
		print(stateHandler)
	end
end

function stateHandler.getAllStats(player)
	print(stateHandler)
	if stateHandler[player] then
		for i, stat in pairs(stateHandler[player]) do
			print(stat)
		end
	end
end

function stateHandler.setStat(player, desiredStat, newValue)
	if stateHandler[player] then
		for i, stat in pairs(stateHandler[player]) do
			if i == desiredStat then
				stat = newValue
			end
		end
	end
end

return stateHandler


What’s exactly is happening, it is your “Kaiser” table that get deleted, or your table you made for your player, where you copied the stats from Kaiser

table created for the player within the playerstable, it loops through kaisers table and gives the children of the table to the newplayerstable

I would have tried this way:

function stateHandler.addToList( player : string , characterName : string )
	if not characterStats[characterName] then
		warn("Did not find valid character")
		return
	end

	if players:FindFirstChild(player) then
		local character = characterStats[characterName]
		if not stateHandler[player] then
			
			
			for i, stat in pairs(character) do
				
				stateHandler[player][i] = stat -- Changed here the way you insert the table, not sure if it can solve your issue
			end 

		end

	else
		warn("passed player was not a valid player "..player.Name)
	end
	print(stateHandler)
end

Hmm, let me try this.

(30 letters laldeldleld)

seems like it didn’t work, It’s still getting deleted, I had this module here because i thought it would help with memory, is there anyway this shouldn’t be working?

What exactly is the data missing.
you mentioned the “playerstable” but i don’t see such thing, only a stateHandler.
If we know what data is missing or deleted, we might see where this data is overwritten.

Second assumption, maybe we can’t edit a Module table, and you will have to stock your StateHandler in a regular script.
We gotta test this first before assuming we can’t keep table in a module. i will run some test on my side, bear with me
After some testing, you can store table in a module.

It’s missing the players newcreated table entirely, i changed the code to match what you said, its printing the table now, but it seems like its lost afterwards once the for loop that sets its stats is done

local players = game:GetService("Players")

local stateHandler = {}

local characterStats = {
	["Kaiser"] = {
		["baseSpeed"] = 12,
		["characterName"] = "Kaiser",
		["chargedShot"] = 0,
		["jumpHeight"] = 7.2,
		["maxStam"] = 100,
		["shotPower"] = 320,
		["stam"] = 100,
		["stun"] = 0,
		["tackleCooldown"] = 0.5,
		["tackleDuration"] = 1,
		["topSpeed"] = 28,
		["CanPossess"] = false,
		["CanRun"] = true,
		["CanShoot"] = true,
		["Possessor"] = false,
		["isTackling"] = false,
	}
}

local playersTable = {}

function stateHandler.addToList( player : string , characterName : string )
	if not characterStats[characterName] then
		warn("Did not find valid character")
		return
	end
	
	if players:FindFirstChild(player) then
		local character = characterStats[characterName]
		if not playersTable[player] then
			playersTable[player] = {}
			local newPlayerTable = playersTable[player]
			
			for i, stat in pairs(character) do
				newPlayerTable[i] = stat
				
			end 
			
		end
		
	else
		warn("passed player was not a valid player "..player.Name)
	end
	print(playersTable)
end

function stateHandler.removeFromList(player : Player)
	
	if playersTable[player] then
		table.remove(playersTable, player)
	end
	
end

function stateHandler.getStat(player : Player, desiredStat : string)
	print(playersTable)
	if playersTable[player] then
		for i, stat in pairs(playersTable[player]) do
			if i == desiredStat then
				print('found your stat')
				return stat
			else
				print(i)
			end
		end
	else
		print(playersTable)
	end
end

function stateHandler.getAllStats(player)
	print(playersTable)
	if playersTable[player] then
		for i, stat in pairs(playersTable[player]) do
			print(stat)
		end
	end
end

function stateHandler.setStat(player, desiredStat, newValue)
	if playersTable[player] then
		for i, stat in pairs(playersTable[player]) do
			if i == desiredStat then
				stat = newValue
			end
		end
	end
end

return stateHandler

That’s weird, it seems to be working fine for me, as it’s shown in the console:


image

Weird, how are you accessing the module script, is it through the server?

local TestingModule = require(script.Parent.ModuleScript)


game.Players.PlayerAdded:Connect(function(Player)
	TestingModule.addToList(Player.Name,"Kaiser")
	
	task.wait(5)
	TestingModule.getStat(Player.Name,"CanRun")
end)

in a server script to test it, and it work fine

Try accessing it through a client in another script and getting a stat and see what it does.

If you mean a LocalScript, that won’t work. the Module is in the ServerScriptService, and localScript can’t require() a script in SSS. So your best luck is to make a RemoteFunction, or a RemoveEvent, that will trigger a ServerScript which will handle the communication between the 2. I recommend a RemoteFunction so you can return the whole player table to the LocalScript.

If you want some help i can try to make a script for you.

I have my script located within replicatedStorage so its both accessible to the client and server, would the servers changes not replicate to the client in the module script? because that seems to be the issue.

if it’s the replicated storage, i don’t know, let me try if it work.

I just realised that the ReplicatedStorage is a Storage, so script won’t run there, so you can’t place a module script inside. You should place it somewhere else.

Like i said i would suggest to place it in SSS, and use a Server Script to communicate with a RemoteFunction with your local script.

repStorage can contain modules and modules function within their, seems like the problem was that client and servers dont share the same version of the module script. sorry for wasting your time but i don’t think theirs any getting around with this, also telling you this if you didn’t know but server storage and rep storage can contain module scripts, server storage should be for server sided scripts because they load instantly, sorry for wasting your time again :sweat_smile:

Thing is, when i try to call the Module located in the Replicated with a local script, it don’t detect it… so i don’t know how you achieved that.

Also, if a client make a modification, it won’t affect the server (in theory) that might be why.
But you should really handle your PlayerTable with a server script.

Also, you are not wasting anyone time. The devForum is here for that, and we all volunteer to help each other, so no, you didn’t wasted my time !

I’ll probably re arrange all this thank you for the pointers tho :+1:

I will try to make the script to handle your module from a local script,
so it’s local < > server < > Module