Ghost Players Ends Up Switching Clothing and Body Colors

I have a game where the goal is to complete obstacle courses as fast as possible. Of course, since characters are opaque by default, a lot of problems are caused because the character and the environment are obstructed by other characters. In order to combat this, I decided to add a “ghost players” system where if you walk close to a character, the character starts to get transparent, which means the character and the environment can only be obstructed by overhead objects, and not other characters.

While this system works, there is one glaring problem that bothers me. There is a bug where other characters’ clothes and body colors get mixed up, which looks awful.

Here’s some footage:

In an attempt to combat this, I decided to give each for loop a different variable to represent value, but that didn’t fix it. I also modified the script to only make the face and the body parts transparent rather than all decals and all parts, but that also didn’t fix it (and I forgot to account for t-shirts oops, so I changed it back to just checking if it was a part or decal). There’s no other solution I can think of at the moment.

Here’s the function:

function updateGhostPlayers()
	local players = SERVICES.PLAYERS:GetPlayers()
	for _, v in pairs(players) do
		if v ~= Player then
			local character = v.Character
			if character ~= nil then
				local delta = Character.Torso.Position - character.Torso.Position
				if GhostPlayers then
					for _, w in pairs(character:GetDescendants()) do
						if w.Name ~= "HumanoidRootPart" then
							if w:IsA("BasePart") or w:IsA("Decal") then
								w.Transparency = 1
							end
						end
					end
				else
					if delta.Magnitude < 15 then
						for _, w in pairs(character:GetDescendants()) do
							if w.Name ~= "HumanoidRootPart" then
								if w:IsA("BasePart") or w:IsA("Decal") then
									w.Transparency = 2.5 / delta.Magnitude
								end
							end
						end
					else
						for _, w in pairs(character:GetDescendants()) do
							if w.Name ~= "HumanoidRootPart" then
								if w:IsA("BasePart") or w:IsA("Decal") then
									w.Transparency = 0
								end
							end
						end
					end
				end
			end
		end
	end
end

EDIT: fixed preformatted text

1 Like