How to fix material flickering

I have a local script that changes the material variant to create a water animation to parts. It uses tags to detect those parts. Here is the bug

Here is the local script in player scripts

local CollectionService = game:GetService("CollectionService") 
local MaterialService = game:GetService("MaterialService")

repeat task.wait() until game:IsLoaded()

--Animated Textures

while true do
	for Index,_ in ipairs(MaterialService.TerrainWater:GetDescendants()) do
		task.wait(.02) -- change speed of the water here lol
		for _,Part in ipairs(CollectionService:GetTagged(script:GetAttribute("TagName"))) do
			if Part:IsA("Part") or Part:IsA("UnionOperation") or Part:IsA("MeshPart") or Part:IsA("BasePart") then
				--Part.Material = Enum.Material.SmoothPlastic
				Part.MaterialVariant = "Water"..Index
			end
		end
	end
end


I haven’t actually looked at the file however it could be flickering due to some material variants not existing

I found out why it is because when the material variant changes the part turn for a split second to original color but it works on the test place its so weird i am checking all the settings on workspace and player scripts so i might find the problem

Maybe create a surface appearance of the previous material variant then make it invisible after a small delay

I’m not sure if you are still working with this or found the solution, but I had the same issue and realized that the original place has a preloaded set of all of the materials already in the workspace. Each iteration the game has to reload the material variant because its not being used by anything in the workspace, but if its being used by a part then it will already be loaded.

3 Likes

Thanks, simple and weird fix but worked perfectly!

1 Like

Large material changes inevitably cause a periodic flicker.

This will be difficult to pull off at that scale. I’d suggest having parts under the baseplate with the materials preset, and then wasting as little time as possible batch updating the water. The better you get it to look, the more overall lag you’re going to add, unfortunately.

Lower-resolution textures and fewer colors will definitely help here.
Preloaded is great, but having a part there with the material is superior.
In this case you’ll need every possible advantage.