Material on Part not updating

Hello! I’m having an issue with a recent script I’ve been working on that disables textures (sets their material to smooth plastic and disables decals and textures) and re-enables them.

Basically what this does is that once it’s enabled, it…
1 - Locates every part under workspace
2 - Inserts itself into a dictionary with the value being their old material (e.g workspace.Part = Enum.Material.Concrete)
3 - Sets the part’s material to SmoothPlastic

It should be assumed how it works once disabled. Within the game I’m making this for, there’s a feature that unloads parts away from the player to save data, StreamingEnabled. The block of code I’m having issue is when enabled and a part is added to workspace, via loading from StreamingEnabled, it wont set the material to the material saved in the dictionary under the same part’s index. Let me know if you have any ideas!

--// Loaded / new part texture reset
	workspace.DescendantAdded:Connect(function(instance)

		if disabled then
			--[[if instance:IsA("BasePart") then
				PartCache[instance] = instance.Material
				instance.Material = Enum.Material.SmoothPlastic
			elseif instance:IsA("Texture") or instance:IsA("Decal") and not instance:IsDescendantOf(Players:GetPlayers().Character) and decals then
				ElementCache[instance] = instance.Transparency
				instance.Transparency = 1
			end]]

		elseif not disabled then -- Block of malfunctioning code vvv
			if instance:IsA("BasePart") and PartCache[instance] ~= nil then -- 1st makes sure part is a basepart and if the part has a material saved
				instance.Material = PartCache[instance] -- Sets part's material from the saved index, this doesnt seem to work but setting other properties of the part seems to for some reason
				table.remove(PartCache, table.find(PartCache, instance)) -- Removes index
			elseif instance:IsA("Texture") or instance:IsA("Decal") and ElementCache[instance] ~= nil then -- Same as above but for decal and texture instances
				instance.Transparency = ElementCache[instance]
				table.remove(ElementCache, table.find(ElementCache, instance))
			end
		end

	end)

I have gotten this script error before as well, there seems. From my Understanding, to be no fix

Just a Roblox bug, have a nice day :wink:

but if this continues wait for a Roblox moderator to respond to this topic

local Enumeration = Enum
local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local Camera = Workspace.CurrentCamera

local Cache = {}
local RenderDistance = 100

local function OnDescendantAdded(Descendant)
	if Descendant:IsA("BasePart") then
		Cache[Descendant] = {Material = Descendant.Material}
	elseif Descendant:IsA("FaceInstance") then
		local BasePart = Descendant:FindFirstAncestorOfClass("BasePart")
		if not BasePart then return end
		Cache[Descendant] = {Transparency = Descendant.Transparency, BasePart = BasePart}
	end
end

local function OnDescendantRemoving(Descendant)
	if Descendant:IsA("BasePart") then
		Descendant.Material = Cache[Descendant].Material
		Cache[Descendant] = nil
	elseif Descendant:IsA("FaceInstance") then
		Descendant.Transparency = Cache[Descendant].Transparency
		Cache[Descendant] = nil
	end
end

local function OnRenderStep()
	for Object, Data in pairs(Cache) do
		if Object:IsA("BasePart") then
			local Distance = (Camera.CFrame.Position - Object.Position).Magnitude
			Object.Material = if Distance > RenderDistance then Enumeration.Material.Plastic else Data.Material
		elseif Object:IsA("FaceInstance") then
			local Distance = (Camera.CFrame.Position - Data.BasePart.Position).Magnitude
			Object.Transparency = if Distance > RenderDistance then 1 else Data.Transparency
		end
	end
end

RunService.RenderStepped:Connect(OnRenderStep)
Workspace.DescendantAdded:Connect(OnDescendantAdded)
Workspace.DescendantRemoving:Connect(OnDescendantRemoving)
for _, Descendant in ipairs(Workspace:GetDescendants()) do
	OnDescendantAdded(Descendant)
end

Thanks for the response! This would make sense as my studio has been buggy all day, mainly UI issues such as their positions not saving and being all white. Anyways, thanks for letting me know!

I will give this a shot, thank you!