How do I make all characters with a certain tag invisible aside from my own character?

You can write your topic however you want, but you need to answer these questions:

  1. I want to make all characters invisible with a certain tag except mine Keep it simple and clear!

  2. MY player becomes invisible as well

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local VisibleEvent = ReplicatedStorage.Events.Visible
local LocalPlayer = Players.LocalPlayer

local hidden = false
local originalTransparency = {}

RunService.RenderStepped:Connect(function()
	for _, player in CollectionService:GetTagged("Playing") do
		if player == LocalPlayer then
			continue
		end
		local character = player.Character
		
		if character ~= LocalPlayer.Character then
			for _, object in ipairs(character:GetDescendants()) do

				if object:IsA("BasePart") or object:IsA("Decal") then
					if hidden then
						if originalTransparency[object] == nil then
							originalTransparency[object] = object.Transparency
						end
						object.Transparency = 1
					else
						if originalTransparency[object] ~= nil then
							object.Transparency = originalTransparency[object]
							originalTransparency[object] = nil
						end
					end

				elseif object:IsA("BillboardGui") then
					if hidden then
						object.Enabled = false
					else
						object.Enabled = true
					end
				end
			end
		end
	end

	-- cleanup destroyed instances
	for object in pairs(originalTransparency) do
		if not object or not object.Parent then
			originalTransparency[object] = nil
		end
	end
end)

VisibleEvent.OnClientEvent:Connect(function(state)
	hidden = (state == "Invisible")
end)

I just want to know why my own local character is becoming invisible too, i only want other player’s characters to be invisible.

On the server, set the Anchored property to true for all players’ HumanoidRootParts—this will set the NetworkOwner of the characters to the server. Then move them to a remote location so they are no longer visible

On the client, set Anchored back to false only for the local player’s character and move it back to the original position, allowing them to move freely on the client side visually

Next, invoke a RemoteFunction to all clients and wait for them to return their positions within, for example, 3 seconds. Otherwise, use their last known location

This will ensure an anticheat mechanism

Okay so, I will do that but will that work if i have other players that are spectating? in that case how would i make it so that spectators see all players like normal but i can still make other players invisible to the local player, im kinda confused on how i would do that or is it not possible?

CollectionService:GetTagged(“Playing”) is returning characters or models, not Player objects.
You then compare them to LocalPlayer, which will never match. You end up being just one of the characters, so you get pushed in too.

if character ~= LocalPlayer.Character then ← one line fix.

lol, my “one line fix” is already in the script .. :man_facepalming:

The tag is added to the player, not the character. and it works fine in my other script when i refer to the tagged players then their characters

Well then use if player ~= LocalPlayer then..
Only a few ways this could go…

You don’t need to keep track of the original transparency of each Part or Decal/Texture. Parts and Decals/Textures have a convenient property called LocalTransparencyModifier that allows you to make them more transparent without affecting the real transparency.

Your own character becomes invisible because it also has the same tag as the other players, and your script treats every tagged instance as a target without correctly excluding your local character, so it applies the invisibility logic to you as well.

Server:

local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local event = ReplicatedStorage.Events.Visible

event.OnServerEvent:Connect(function(player, state)
	for _, other in ipairs(Players:GetPlayers()) do
		if other ~= player then
			local character = other.Character
			if character then
				if state == "Invisible" then
					CollectionService:AddTag(character, "Playing")
				else
					CollectionService:RemoveTag(character, "Playing")
				end
			end
		end
	end

	event:FireClient(player, state)
end)

Local:

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local VisibleEvent = ReplicatedStorage.Events.Visible
local LocalPlayer = Players.LocalPlayer

local hidden = false
local originalTransparency = {}

RunService.RenderStepped:Connect(function()
	if hidden then
		for _, character in ipairs(CollectionService:GetTagged("Playing")) do
			-- - skip my own character so it never gets hidden
			if character == LocalPlayer.Character then
				continue
			end

			for _, object in ipairs(character:GetDescendants()) do
				if object:IsA("BasePart") or object:IsA("Decal") then
					if originalTransparency[object] == nil then
						originalTransparency[object] = object.Transparency
					end
					object.Transparency = 1
				elseif object:IsA("BillboardGui") then
					object.Enabled = false
				end
			end
		end
	else
		for object, transparency in pairs(originalTransparency) do
			if object and object.Parent then
				object.Transparency = transparency
			end
			originalTransparency[object] = nil
		end
	end
end)

VisibleEvent.OnClientEvent:Connect(function(state)
	hidden = state == "Invisible"
end)

Okay first of all the server should not be running any code to facilitate making players invisible. It should only be firing to the client to make players invisible/visible.

Also you shouldn’t be doing this every frame, it becomes uneedingly expensive. Use CharacterAdded and DescendantAdded to make them invisible instead.

And one more thing, you don’t need tags at all! You can just get the players directly.

My implementation

PlayerHandler - LocalScript (StarterPlayerScripts)

Summary
-- for handling server requests

local replicatedStorage = game:GetService("ReplicatedStorage")
local setPlayerVisibleEvent: RemoteEvent = replicatedStorage.SetPlayerVisible -- remote event
local PlayerModifier = require(replicatedStorage.PlayerModifier) -- modulescript

setPlayerVisibleEvent.OnClientEvent:Connect(PlayerModifier.setVisibility)

PlayerModifier - ModuleScript (ReplicatedStorage)

Summary
local players = game:GetService("Players")
local localPlayer: Player = players.LocalPlayer

--

local PlayerModifier = {}

local connections = {}
PlayerModifier.areOthersVisible = false -- used in onDescendantAdded() + allow external LocalScripts to view variable

--// Connections for invisible
local function onDescendantAdded(obj: Instance)
	--// Set transparency on basepart add
	if not obj:IsA("BasePart") then return end
	obj.Transparency = PlayerModifier.areOthersVisible and 0 or 1
end

local function onCharacterAdded(character: Model)
	character.DescendantAdded:Connect(onDescendantAdded)
	for _, obj: Instance in character:GetDescendants() do
		onDescendantAdded(obj)
	end
end

local function onPlayerAdded(player: Player)
	table.insert(connections, player.CharacterAdded:Connect(onCharacterAdded))
	player.CharacterAdded:Connect(onCharacterAdded)
	if player.Character then
		onCharacterAdded(player.Character)
	end
end
------------------------------

function PlayerModifier.setVisibility(visible: boolean)
	print(visible) -- test print!
	PlayerModifier.areOthersVisible = visible -- let the rest of the module know 
	if visible then
		--// Remove connections
		for _, connection: RBXScriptConnection in connections do
			connection:Disconnect()
			connection = nil
		end
		table.clear(connections)
		
		--// Make visible
		for _, player: Player in players:GetPlayers() do
			if player == localPlayer then continue end
			
			local character: Model? = player.Character
			if not character then continue end
			
			for _, obj: Instance in character:GetDescendants() do
				onDescendantAdded(obj) -- areOthersVisible is already set to true, so parts will be transparency 0
			end
		end
	else
		--// Make invisible
		table.insert(connections, players.PlayerAdded:Connect(onPlayerAdded))
		for _, player in players:GetPlayers() do
			if player == localPlayer then continue end
			onPlayerAdded(player)
			--[[
				areOthersVisible is set to false, the player's
				current & future character + any joining players
				will have their parts set to transparency 1
			]]
		end
	end
end

return PlayerModifier

You also need a RemoteEvent named SetPlayerVisible in ReplicatedStorage.

Demo game file:
TestGame.rbxl (71.4 KB)

Now from the client you can make others invisible/visible with this code

local replicatedStorage = game:GetService("ReplicatedStorage")
local PlayerModifier = require(replicatedStorage.PlayerModifier)

PlayerModifier.setVisibility(true) -- or false!!

And from the server:

local replicatedStorage = game:GetService("ReplicatedStorage")
local setPlayerVisibleEvent: RemoteEvent = replicatedStorage.SetPlayerVisible

setPlayerVisibleEvent:FireClient(player, true) -- or false

If you want it to save, add another RemoteEvent and fire it from the client inside setVisibility(). Then when the player joins, Invoke from the server to get the saved data and call setVisibility() from the LocalScript.

okay! thank you very much :folded_hands:. Im probably gonna have to edit the code to fit my game but this will work well.