The table becomes empty

Here’s my code

--!strict

local SettingDataController = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataTypes = require(ReplicatedStorage.Types.DataTypes)
local SettingDataTypeEnum = require(ReplicatedStorage.Enums.Data.SettingDataTypeEnum)
local SettingDataUpdateEvent = ReplicatedStorage.Events.SettingDataUpdateEvent

local settingData: DataTypes.SettingData = {}

function SettingDataController.OnStart()
	SettingDataUpdateEvent.OnClientEvent:Connect(function(dataType: SettingDataTypeEnum.SettingDataTypeEnum, value: DataTypes.PlayerData)
		settingData[dataType] = value
	end)
end

function SettingDataController.getSettingData(dataType: SettingDataTypeEnum.SettingDataTypeEnum): any
	if not settingData then
		warn("SettingDataController: Attempted to get setting before data was initialized")
		return nil
	end

	local value = settingData[dataType]

	return value
end

function SettingDataController.updateSettingData(
	settingDataType: SettingDataTypeEnum.SettingDataTypeEnum,
	value: DataTypes.SettingData
): boolean
	if not settingData then
		warn("SettingDataController: Attempted to update setting before data was initialized")
		return false
	end

	settingData[settingDataType] = value

	SettingDataUpdateEvent:FireServer(settingDataType, value)

	return true
end

return SettingDataController

And I used this framework to load this module.

--!strict

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local RunService = game:GetService("RunService")
local StarterPlayer = game:GetService("StarterPlayer")
local StarterPlayerScripts = StarterPlayer.StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ClientLoadedEvent: RemoteEvent = ReplicatedStorage.Events.ClientLoadedEvent
local IsServer = RunService:IsServer()
local RootDirectory = if IsServer then ServerScriptService else Players.LocalPlayer:WaitForChild("PlayerScripts")
local ModuleDirectory = if IsServer then RootDirectory.Handlers else RootDirectory:WaitForChild("Controllers")

local function RequireModule(moduleScript: Instance)
	task.spawn(function()
		if moduleScript:IsA("ModuleScript") then
			local success, import = pcall(function()
				return require(moduleScript)
			end)

			if not success then
				warn("Error while loading module:", moduleScript:GetFullName())
			else
				print("Successfully loaded module:", moduleScript:GetFullName())

				local onStart = import.OnStart
				if onStart then
					onStart()
				end
			end
		end
	end)
end

return function()
	if IsServer then
		for _, child in ModuleDirectory:GetDescendants() do
			RequireModule(child)
		end
	else
		for _, child in StarterPlayerScripts.Controllers:GetDescendants() do
			RequireModule(child)
		end

		ClientLoadedEvent:FireServer()
	end
end

And when I use getSettingData function it returns nil.
So I checked the settingData table when I use it, the table was {}.
Is lua garbage collecting my table?

1 Like

I found the problem.
The problem was each module that requires SettingDataController is using each instances.
So it means using other settingData table.

Then how can I fix this.
How can I make to use same module.

1 Like

why do you need that?
Its just beyond stuipid.
Just use require()

2 Likes

I used require() but each script requires different SettingDataController Instance.
So module script isn’t sharing local variable.

Oh I found the bug lol

ModuleLoader loads module script from StarterPlayerScripts
And my script loads module from PlayerScripts

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