Local script not turning players transparent

Ok so I want to make a button that will hide other players for my obby game, locally.
However my code does not work, I’ve searched online on how to do it but they used the same method. I am also getting “changing transparency” in the output. Here’s the code:

	if playersHidden == false then
		--make players invisible
		print("hiding players")
		playersHidden = true
		for i,plr in pairs(players:GetPlayers()) do
			local char = plr:FindFirstChild("Character")
			if char and plr ~= player then
				for _,Bodypart in pairs(char:GetDescendants()) do
					if Bodypart:IsA("BasePart") then
						print("changing transparency")
						Bodypart.Transparency = 0.85
					elseif Bodypart:IsA("Accessory") then
						print("changing transparency")
						Bodypart.Handle.Transparency = 0.85
					end
				end
			end
		end
		
	elseif playersHidden == true then
		--make players visible
		playersHidden = false
		for i,plr in pairs(players:GetPlayers()) do
			local char = plr:FindFirstChild("Character")
			if char and plr ~= player then
				for _,Bodypart in pairs(char:GetDescendants()) do
					if Bodypart:IsA("BasePart") then
						Bodypart.Transparency = 0
					elseif Bodypart:IsA("Accessory") then
						Bodypart.Handle.Transparency = 0
					end
				end
			end
		end
	end
end)
1 Like

You mean transparent or invisible?

I want them to be transparent, not completely invisible (0.85 Transparency)

1 Like

Sorry about the format I fixed it to make it easier to read, also bump.

Is it the ~= player part? It should be ==

Try testing this code out:

local players = game:GetService("Players")
local player = players.LocalPlayer

local playersHidden = false

local TRANSPARENCY_MULT = 0.85

local function modifyModelTransparency(target : Model, multiplier : number)
	if (target.Parent ~= nil) then -- ensure model is still valid
		for _, part in ipairs(target:GetDescendants()) do
			if (part:IsA("BasePart")) then
				part.LocalTransparencyModifier = multiplier
			end
		end 
	end
end

local function togglePlayerTransparency()
	playersHidden = not playersHidden
	
	-- // multiplier = playersHidden{T : TRANSPARENCY_MULT, F : 0}
	local multiplier = (playersHidden and TRANSPARENCY_MULT) or 0
	
	for _, targetPlayer in ipairs(players:GetPlayers()) do
		if (targetPlayer ~= player) then
			modifyModelTransparency(targetPlayer.Character, multiplier)
		end
	end
end

togglePlayerTransparency()

What this code does:

  1. Creates a function togglePlayerTransparency() that toggles the playersHidden boolean and then changes the value of a multiplier. When playersHidden is true, the value is equal to TRANSPRENCY_MULT; otherwise, the value is 0. It then loops through every player (except your own) and calls the modifyModelTransparency function for each of their characters.

  2. Creates a function modifyModelTransparency() that loops through all of the BaseParts of a target model and sets the LocalTransparencyModifier to the given multiplier.

This approach should fix your issues and make the code run faster on the client.

It worked, thank you. I don’t know what the issue was on my part but next time I will split my code up into functions so its easier to view and catch errors.

1 Like

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