Problem with table "resetting" what's inside

So I was programming a settings gui and some of the tables (em,Controllers,PopulationCodes) say that it has nothing inside it even though I clearly added functions to the table. I’ve been trying to figure out why since yesterday but it’s making no sense to me.

If anyone could please tell me the mistaking here then that’d be great.

--Module:
local module = {}

local em = {}
local dataStuffs = {}

local function ThemeControl(mode)
	dataStuffs.Theme = mode
	return "Hi"
end
local function CloudsControl(mode)
	dataStuffs.Clouds = mode
	return "Konnichiwa"
end
local function ShadowsControl(mode)
	dataStuffs.Shadows = mode
	return "Hola"
end
warn(ThemeControl("Black Magic"),CloudsControl("On"),ShadowsControl("On")) --Hi,Konnichiwa,Hola

local Controllers = {Theme = ThemeControl,Clouds = CloudsControl,Shadows = ShadowsControl}
warn(Controllers,#Controllers,"Controllers") --table,0,Controllers
local PopulationCodes = {["Clouds"] = {"On","Off"},["Shadows"] = {"On","Off"}}
warn(PopulationCodes,#PopulationCodes,"PopulationCodes") --table,0,PopulationCodes

function module.Init(funcs)
	em = funcs -- Functions for RemoteEvent (There is not 0 functions in this table)
	warn(#em,"em") --0,em
	
	local TempTable = {"hi","reee",function() warn("hi") end}
	warn(TempTable,"TempTable",#TempTable) --table,TempTable,3
	
	local ThemeData = {}
	PopulationCodes["Theme"] = ThemeData
	warn(PopulationCodes,"PopulationCodes",#PopulationCodes) --table,PopulationCodes,0
	
	function em.RecieveFrameData(theme,clouds,shadows)
		warn(theme,clouds,shadows,"RecieveFrameData")--Black Magic,On,On,RecieveSettingsFrameData
		dataStuffs = {{Name = "Theme",Value = theme},{Name = "Clouds",Value = clouds},{Name = "Shadows",Value = shadows}}
		warn(dataStuffs,"dataStuffs",#dataStuffs) --table,dataStuffs,3
		if not dataStuffs then return end
		
		local lOrder = 0
		for _,v in ipairs(dataStuffs) do
			lOrder = lOrder + 1
			warn(v.Name,lOrder)
		end
	end
	
	em.RecieveFrameData("Black Magic","On","On")--Placeholder for Event:FireServer() (Since it will get a players settings datastore)
end

return module

--Client:
local em = {}
function em.func() -- just to make there be 1 thing inside the em inside of 0
	warn("temp")
end

local module = require(script.ModuleScript)
module.Init(em)

Output looks like this

that function is outside of the module, you really arent returning anything because module is empty

you have to do like this

function module.ThemeControl(mode)
        dataStuffs.Theme = mode
	return "Hi"
end
1 Like

Not what I meant when I said

I clearly added functions to the table

What I was talking about was the tables “em” ,“Controllers” and “PopulationCodes” and how the output says they have nothing in them even though I added function and tables into them when they were made.

The reason why #Controllers, #PopulationCodes and #em return 0 is because the # operator is used to get the length of an array while these tables are dictionaries.
To get the length of a dictionary you can loop through the table and count how often it’ll iterate.
Example:

local PopulationCodes = {["Clouds"] = {"On","Off"},["Shadows"] = {"On","Off"}}

local function getDictionaryLength(dictionary)
	local length = 0
	for _ in pairs(PopulationCodes) do
		length = length + 1
	end
	return length
end

print(PopulationCodes, getDictionaryLength(PopulationCodes), "PopulationCodes") -- table, 2, PopulationCodes
2 Likes