Contents of my table are being removed without me using table.remove

I am trying to make 2 settings, one to disable and enable sound and one to disable and enable Materials

Before disabling materials
Screenshot 2024-10-10 181917

After disabling materials
Screenshot 2024-10-10 181940

Before disabling sound
Screenshot 2024-10-10 182529

After disabling sound
Screenshot 2024-10-10 182551

Code to add the data (Server)

task.spawn(function()
		local descendants = game:GetDescendants()
    for i = 1, #descendants do
        local obj = descendants[i]
        if obj:IsA("BasePart") then
            TablesModule.InsertMaterial(obj)
        elseif obj:IsA("Sound") then
            TablesModule.InsertVolume(obj)
        end
		end
		
		TablesModule.PrintMaterialAmount()
end)

Code accessing the data (Local)

menu_settings_buttons.Materials.Button.Activated:Connect(function()
	if MaterialsOn then
		MaterialsOn = false

		task.spawn(function()
			effect_3_pos_off:Play()
			effect_3_color_off:Play()
		end)

		task.spawn(function()
			local descendants = workspace:GetDescendants()
			for i = 1, #descendants do
				local obj = descendants[i]
				if obj:IsA("BasePart") then
					obj.Material = Enum.Material.SmoothPlastic
				end
			end
		end)
	else
		MaterialsOn = true

		task.spawn(function()
			effect_3_pos_on:Play()
			effect_3_color_on:Play()
		end)
		
		if next(MaterialTable) then
			print("MaterialTable is populated")
			TablesModule.PrintMaterialAmount()
		else
			print("MaterialTable is empty")
			TablesModule.PrintMaterialAmount()
		end
		
		task.spawn(function()
			local descendants = workspace:GetDescendants()
			for i = 1, #descendants do
				local obj = descendants[i]
				if obj:IsA("BasePart") then
					local material = MaterialTable[obj]
					if material then
						obj.Material = material
					end
				end
			end
		end)
	end
end)

menu_settings_buttons.Sound.Button.Activated:Connect(function()
	if SoundOn then
		SoundOn = false

		task.spawn(function()
			effect_4_pos_off:Play()
			effect_4_color_off:Play()
		end)

		local descendants = workspace:GetDescendants()
		VolumeTable = {}
		for i = 1, #descendants do
			local obj = descendants[i]
			if obj:IsA("Sound") then
				obj.Volume = 0
			end
		end

		-- Check if VolumeTable is empty before playing sound
		if next(VolumeTable) then
			print("VolumeTable is populated")
			TablesModule.PrintVolumeAmount()
		else
			print("VolumeTable is empty")
			TablesModule.PrintVolumeAmount()
		end
	else
		SoundOn = true

		task.spawn(function()
			effect_4_pos_on:Play()
			effect_4_color_on:Play()
		end)

		local descendants = workspace:GetDescendants()
		for i = 1, #descendants do
			local obj = descendants[i]
			if obj:IsA("Sound") then
				local volume = VolumeTable[obj]
				if volume then
					obj.Volume = volume
				end
			end
		end
	end
end)

Code that contains all the data (Module)

local tables = {}

tables.MaterialTable = {}
tables.VolumeTable = {}

function tables.InsertMaterial(obj: BasePart)
	tables.MaterialTable[obj] = obj.Material
end

function tables.InsertVolume(obj: Sound)
	tables.VolumeTable[obj] = obj.Volume
end

function tables.PrintMaterialAmount()
	local count = 0
	for _, _ in pairs(tables.MaterialTable) do
		count += 1
	end
	print("The amount of materials in the table are "..count)
end

function tables.PrintVolumeAmount()
	local count = 0
	for _, _ in pairs(tables.VolumeTable) do
		count += 1
	end
	print("The amount of volume data in the table are "..count)
end

return tables

I have already put many prints to see what was the issue

I have looked on the developer form for solutions and haven’t found anything

1 Like

I’m guessing that it’s not properly reverting the textures back on the Client after disabling then re-enabling the materials?

One thing I noticed is that the Textures are being stored by the Server via a Server Script, presumably into a ModuleScript. ModuleScripts do not share the modifications done by either the Client or the Server, so you would need to store these materials via a LocalScript(on the Client) if you were to store them in a ModuleScript.

2 Likes

Yea i noticed that after printing how many materials were in the table in the local script

2 Likes

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