Why does this GetPartsInPart loop not work like I expected it?

I wanted this getpartsinpart loop to get all the parts inside a sphere and turn their transparency to 1 if they’re of a character’s.

local chr = plr.Character
	local remotevent = game.ReplicatedStorage.RemoteEvents.Suke.Invisible
	local hum = chr:FindFirstChildOfClass("Humanoid")
	local health = hum.Health
	local Sphere = script.Sphere:Clone()
	Sphere.Parent = workspace.Effects
	Sphere.Position = chr.HumanoidRootPart.Position
	Sphere.Size = Vector3.new(50.75, 50.75, 50.75)
	
	
	local ended = false
	local touched = game.Workspace:GetPartsInPart(Sphere)
	local tabletargets = {}
	
	for i, touch in pairs(touched) do
			if touch.Parent:FindFirstChildOfClass("Humanoid") then
				local chr = touch.Parent
				if not table.find(tabletargets, chr) then
					for i, c in pairs(chr:GetDescendants()) do
						if (c.Name ~= "HumanoidRootPart" and c:IsA("BasePart") or c:IsA("Decal") and c.Name ~= "HumanoidRootPart") then
							c.Transparency = 1
							if c:IsA("BasePart") then
								c.Material = Enum.Material.Glass
							end
						elseif c:IsA("BillboardGui") then
							c.Enabled = false
						end
					end
					wait(1)
					table.insert(tabletargets, chr)
					print("NPCed LOL noob")
					wait(10)
					for i, c in pairs(chr:GetDescendants()) do
						if (c.Name ~= "HumanoidRootPart" and c:IsA("BasePart") or c:IsA("Decal") and c.Name ~= "HumanoidRootPart") then
							if c:IsA("BasePart") then
								c.Material = Enum.Material.SmoothPlastic
							end
							c.Transparency = 0
						elseif c:IsA("BillboardGui") then
							c.Enabled = true
						end
					end
				end
			end
	end

The problem is, if I’m in the sphere with an NPC then it’ll set my transparency first and then the NPC’s.
See for yourself: https://gyazo.com/de2993e326b7aea1d3db0dfd4e09b8e7
10 seconds later:

It does not work as you expected because you expected the for loop which detects the character to keep going after finding a suitable character to turn invisible.

It doesn’t because it will have to wait(10) before it turns visible again.

You can solve it with a simple task.spawn or coroutine.wrap to keep the forloop going and ignore the yield for turning the character visible again.

local chr = plr.Character
	local remotevent = game.ReplicatedStorage.RemoteEvents.Suke.Invisible
	local hum = chr:FindFirstChildOfClass("Humanoid")
	local health = hum.Health
	local Sphere = script.Sphere:Clone()
	Sphere.Parent = workspace.Effects
	Sphere.Position = chr.HumanoidRootPart.Position
	Sphere.Size = Vector3.new(50.75, 50.75, 50.75)
	
	
	local ended = false
	local touched = game.Workspace:GetPartsInPart(Sphere)
	local tabletargets = {}
	
	for i, touch in pairs(touched) do
			if touch.Parent:FindFirstChildOfClass("Humanoid") then
				local chr = touch.Parent
				if not table.find(tabletargets, chr) then
					for i, c in pairs(chr:GetDescendants()) do
						if (c.Name ~= "HumanoidRootPart" and c:IsA("BasePart") or c:IsA("Decal") and c.Name ~= "HumanoidRootPart") then
							c.Transparency = 1
							if c:IsA("BasePart") then
								c.Material = Enum.Material.Glass
							end
						elseif c:IsA("BillboardGui") then
							c.Enabled = false
						end
					end

					table.insert(tabletargets, chr)

task.spawn(function(
					print("NPCed LOL noob")
					wait(10)
					for i, c in pairs(chr:GetDescendants()) do
						if (c.Name ~= "HumanoidRootPart" and c:IsA("BasePart") or c:IsA("Decal") and c.Name ~= "HumanoidRootPart") then
							if c:IsA("BasePart") then
								c.Material = Enum.Material.SmoothPlastic
							end
							c.Transparency = 0
						elseif c:IsA("BillboardGui") then
							c.Enabled = true
						end
					end
end)
				end
			end
	end

2 Likes

Thanks for this, but it doesn’t destroy the sphere.

Edit: Nevermind, I think i know now.

1 Like