How to make a texture toggle

hey, I was making a settings system, essentially I need to texture toggle system, so when it’s toggled, it gets every texture ingame and just clears it, and when it’s toggled the other way, it makes them all appear again. I already have the base code for the toggling, it’s just the removing the textures then adding them back.

for context this is NOT materials, actual textures.

thanks :tongue:

You could use collection service and loop through all the descendants of workspace who have the “Texture” tag.

what about making them reappear then

You could also have a string attribute along with the tag that has the original texture and if the user reapplies textures in settings, you can loop through and set the texture back using the texture string.

for i, texture in ipairs(workspace:GetDescendants) do
   local textureId = texture:GetAttribute("TextureId")
   texture.TextureID = textureId
1 Like

Ha, guess you’re not working with terrain. Deleted
This isn’t going to work :rofl:

CollectionService for both performance and streaming purposes. Example code that runs on the client:

--!strict

local CollectionService = game:GetService("CollectionService")

local TEXTURE_TAG: string = "Texture"
local TEXTURES_ENABLED: boolean = true

-- function to show/hide texture depending if it has the TEXTURE_TAG
-- and if the TEXTURES_ENABLED is true
local function updateTextureLocalTransparency(texture: Texture): ()
	if texture.ClassName == "Texture" then
		texture.LocalTransparencyModifier = if texture:HasTag(TEXTURE_TAG) == false or (texture:HasTag(TEXTURE_TAG) == true and TEXTURES_ENABLED == true) then 0 else 1
	end
end

-- listens for instance being added/removed with the TEXTURE_TAG
CollectionService:GetInstanceAddedSignal(TEXTURE_TAG):Connect(updateTextureLocalTransparency)
CollectionService:GetInstanceRemovedSignal(TEXTURE_TAG):Connect(updateTextureLocalTransparency)

-- manually run function for existing instance with the TEXTURE_TAG 
for _, instance: Instance in CollectionService:GetTagged(TEXTURE_TAG) do
	task.spawn(updateTextureLocalTransparency, instance :: Texture)
end



-- Example button event to toggle textures
someButton.Activated:Connect(function(): ()
	TEXTURES_ENABLED = not TEXTURES_ENABLED
	
	for _, instance: Instance in CollectionService:GetTagged(TEXTURE_TAG) do
		task.spawn(updateTextureLocalTransparency, instance :: Texture)
	end
end)
-- I would recommend manually tagging textures with the TEXTURE_TAG in studio
-- for performance reasons. Otherwise, you can call this function to automatically
-- add the tag to your textures in the workspace (not recommended as it may take
-- longer the larger your workspace).
local function tagTexturesAutomatically(): ()
	local function onDescendantAdded(descendant: Instance): ()
		if descendant.ClassName == "Texture" then
			local texture: Texture = descendant :: Texture
			texture:AddTag(TEXTURE_TAG)
		end
	end

	for _, descendant: Instance in workspace:GetDescendants() do
		task.spawn(onDescendantAdded, descendant)
	end

	workspace.DescendantAdded:Connect(onDescendantAdded)
end

tagTexturesAutomatically()

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