GetPartsInPart() loop not working as expected

I’ve probably said this so many times but, I wanna make an ability that releases a sphere, and the parts that hit it, if it’s of a character’s then it detects wether the part is a descendant of NPC or Players folder in workspace. Similarly, if it’s a descendant of the Players folder then it’s of a player’s, the same goes for NPCs. But, it’s not working as I expected it would. If the player is a little far from npcs or players(but the npc or player is still touching the sphere when it’s done tweening), then only the player would go invisible, and sometimes a few seconds later the other players or npcs touching the sphere even after it gets destroyed goes invisible.

Let me show you what I mean: https://gyazo.com/b5c62df6df5997316419199060799401

If the player is near: https://gyazo.com/abb2d6753f2d6d768cd99a8982f4b808

sorry if I had bad explaination I’m not good at explaining

Can you please provide your code…??

Oh! Sorry, I thought I already pasted it in xD

local functions = {}

function functions.Start(plr)
	local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
	local tweenservice = game:GetService("TweenService")
	local chr = plr.Character
	local remote = game.ReplicatedStorage.RemoteEvents.Suke.Invisible
	local hum = chr:FindFirstChildOfClass("Humanoid")
	local health = hum.Health
	local cube = script.cube:Clone()
	cube.Parent = workspace.Effects
	cube.Position = chr.HumanoidRootPart.Position
	local tween = tweenservice:Create(cube, tweeninfo, {Size = Vector3.new(50.75, 50.75, 50.75)})
	
	tween:Play()
	tween.Completed:Wait()
	
	local debounce = false
	local aoe = workspace:GetPartsInPart(cube)
	local targets = {}

	
	for i, a in pairs(aoe) do
		if a:IsA("BasePart") and a.Name ~= "HumanoidRootPart" then
			if a:IsDescendantOf(workspace.NPCs) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d.Name ~= "HumanoidRootPart" and d:IsA("BasePart") or d:IsA("Decal") and d.Transparency == 0 and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							if debounce == false then
								debounce = true
								table.insert(targets, a.Parent)
								print("STARTED NPC")
							end
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d.Name ~= "HumanoidRootPart" and d:IsA("BasePart") or d:IsA("Decal") and d.Transparency == 1 and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
						end
						local newtween = tweenservice:Create(cube, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
						newtween:Play()
						newtween.Completed:Wait()
						cube:Destroy()
						if debounce == false then
							debounce = true
							print("ENDED NPC")
						end
					end
				end
			elseif a:IsDescendantOf(workspace.Players) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d.Name ~= "HumanoidRootPart" and d:IsA("BasePart") or d:IsA("Decal") and d.Transparency == 0 and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
								remote:InvokeClient(game.Players:GetPlayerFromCharacter(a.Parent), 0.7)
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							if debounce == false then
								debounce = true
								table.insert(targets, a.Parent)
								print("STARTED PLR")
							end
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d.Name ~= "HumanoidRootPart" and d:IsA("BasePart") or d:IsA("Decal") and d.Transparency == 1 and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
						end
						local newtween = tweenservice:Create(cube, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
						newtween:Play()
						newtween.Completed:Wait()
						cube:Destroy()
						if debounce == false then
							debounce = true
							print("ENDED PLR")
						end
					end
				end
			end
		end
	end
	
end

return functions
local aoe = workspace:GetPartsInPart(cube)

You are only determining the players touching the part at the very start. You should also add a .Touched event to the script!

1 Like

so from looking at ur issue i remembered that region3 (which is now technically deprecated) and getpartsinpart have the same issue. if u try to find multiple entities inside of the said part it will only find the first entity, the rest would be ignored. U should instead use touched event and disconnect it when it hits anything and get all of the touching parts. u could also go a bit more wonky and make a table of every npc/player it hit and loop through and delete the said player/npc if it finds it in the loop

obviously with the table method u arent required to use touched but its ur call

so how would I make it like expected then?

I converted it into gettouchingparts and touched event.

Although, another problem occured.

It detects if it’s an npc or player’s bodypart and then only sets 1 body part transparency into 0. and also if I’m in the sphere with the npc then i go invisible seconds after that 1 body part became invisible and visible again.

local functions = {}

function functions.Start(plr)
	local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
	local tweenservice = game:GetService("TweenService")
	local chr = plr.Character
	local remote = game.ReplicatedStorage.RemoteEvents.Suke.Invisible
	local hum = chr:FindFirstChildOfClass("Humanoid")
	local health = hum.Health
	local cube = script.cube:Clone()
	cube.Parent = workspace.Effects
	cube.Position = chr.HumanoidRootPart.Position
	local tween = tweenservice:Create(cube, tweeninfo, {Size = Vector3.new(50.75, 50.75, 50.75)})
	
	tween:Play()
	tween.Completed:Wait()
	
	local function GetTouchingParts(part)
		local connection = part.Touched:Connect(function() end)
		local results = part:GetTouchingParts()
		connection:Disconnect()
		return results
	end
	
	local debounce = false
	local aoe = GetTouchingParts(cube)
	local targets = {}
	
	for i, a in pairs(aoe) do
		if a:IsDescendantOf(workspace.NPCs) then
			local parent = a.Parent
			if not table.find(targets, parent) then
				for i, p in pairs(parent:GetDescendants()) do
					if (p:IsA("BasePart") or p:IsA("Decal") and p.Name ~= "HumanoidRootPart" and p.Transparency ~= 1) then
						local tween = tweenservice:Create(p, tweeninfo, {Transparency = 1})
						tween:Play()
					elseif p:IsA("BillboardGui") then
						p.Enabled = false
					end
				end
			end
			table.insert(targets, parent)
			print("NPC")
			wait(10)
			if table.find(targets, parent) then
				for i, p in pairs(parent:GetDescendants()) do
					if (p:IsA("BasePart") or p:IsA("Decal") and p.Name ~= "HumanoidRootPart" and p.Transparency ~= 0) then
						local tween = tweenservice:Create(p, tweeninfo, {Transparency = 0})
						tween:Play()
					elseif p:IsA("BillboardGui") then
						p.Enabled = true
					end
				end
			end
		elseif a:IsDescendantOf(workspace.Players) then
			local parent = a.Parent
			local playar = game.Players:GetPlayerFromCharacter(parent)
			if not table.find(targets, parent) then
				for i, p in pairs(parent:GetDescendants()) do
					if (p:IsA("BasePart") or p:IsA("Decal") and p.Name ~= "HumanoidRootPart" and p.Transparency ~= 1) then
						local tween = tweenservice:Create(p, tweeninfo, {Transparency = 1})
						tween:Play()
						remote:InvokeClient(playar, 0.7)
					elseif p:IsA("BillboardGui") then
						p.Enabled = false
					end
				end
			end
			table.insert(targets, parent)
			print("Player")
			wait(10)
			if table.find(targets, parent) then
				for i, p in pairs(parent:GetDescendants()) do
					if (p:IsA("BasePart") or p:IsA("Decal") and p.Name ~= "HumanoidRootPart" and p.Transparency ~= 0) then
						local tween = tweenservice:Create(p, tweeninfo, {Transparency = 0})
						tween:Play()
					elseif p:IsA("BillboardGui") then
						p.Enabled = true
					end
				end
			end
		end
	end
	

	
end

return functions

If u see here, only my torso was affected. https://gyazo.com/e2e780addeab685f28ec4692e81da7b5