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?