Script does not print the names of the parts returned by FindPartsInRegion3

Hi!

So I want to create an EMP gun that disables every light around the player, but the problem is that my script doesn’t print the lights’ names.

Script(ServerScriptService):

local REAction = RS:WaitForChild("EMPAction")

local objects = game.Workspace:GetDescendants()

local lights = {}

for i = 1, #objects do
	if objects[i].ClassName == "PointLight" or objects[i].ClassName == "SpotLight" or objects[i].ClassName == "SurfaceLight" then
		print("Found light object!") -- This prints 3 times as I have one light object in 3 parts
		table.insert(lights, objects[i])
	end
end

REAction.OnServerEvent:Connect(function(player, pos, size) -- pos is the position of HumanoidRootPart and size is its size; both are sent and received successfully
	if canEMPServer == true then
		local area = Instance.new("Part")
		area.Name = "Area "..player.Name
		area.Size = Vector3.new(50, 50, 50)
		area.Parent = workspace
		area.CanCollide = false
		area.Transparency = 0.5
		area.Anchored = true
		area.Position = pos
        -- the part is created successfully with those properties
		wait()
		print(area.Size)
		print(size)
		--those 2 print successfully
		local parts = workspace:FindPartsInRegion3WithWhiteList(Region3.new(size, area.Size), lights, 20)
		wait()
		for i, v in pairs(parts) do
			print(v.Name)  -- here nothing prints
		end
	else
		print("Can not EMP Server")
	end
end)

As you can see in the screenshot above, there are 3 parts, each with a light object inside, the part gets created successfully but it only prints the part’s Size and the HumanoidRootPart’s Size without also printing the objects’ names inside parts.

Does anyone have any idea why this is happening?

If you’re trying to make the region the size of the part try this:

local parts = workspace:FindPartsInRegion3WithWhiteList(Region3.new(area.Position-(area.Size/2), area.Position+(area.Size/2)), lights, 20)

I’ve tried and it still doesn’t print.

Increase the number 20. You can do something like math.huge.

I did and still there’s nothing in the output.

Scripts

Emp Server

local EmpHitboxSize = Vector3.new(50, 50, 50)
local DisabledTime = 5
local MaxHitboxIndex = 100
local MaxDescendantsIndex = 50
local UpdateInterval = 0.02
local EmpSound = "rbxassetid://335657174"
local EmpSound_Volume = 1
local EmpSound_RollOffMaxDistance = 10000
local EmpSound_PlaybackSpeed = 1
local DisabledEmitter = script.DisabledFX
local RemoteFunctionCooldown = 2
local VisualEmpColor = Color3.fromRGB(13, 67, 168)
local VisualEmpTransparency = 0.6
local VisualEmpLifetime = 0.5



local CastEmpRemote = Instance.new("RemoteFunction")
CastEmpRemote.Name = "CastEmp"
CastEmpRemote.Parent = game.ReplicatedStorage


local DisabledLights = {}
local function Find(Light)
	for i = 1, #DisabledLights do
		if DisabledLights[i][1] == Light then
			return i
		end
	end
	return nil
end


local function Update()
	for i = 1, #DisabledLights do
		if DisabledLights[i] == nil then continue end
		if DisabledLights[i][1] == nil then table.remove(DisabledLights, i) continue end
		if tick() >= DisabledLights[i][2] then
			DisabledLights[i][1].Enabled = true
			if DisabledLights[i][3] ~= nil then
				DisabledLights[i][3]:Destroy()
			end
			table.remove(DisabledLights, i)
		else
			DisabledLights[i][1].Enabled = false
		end
	end
end


coroutine.wrap(function()
	local LastUpdate = tick()
	Update()
	game:GetService("RunService").Heartbeat:Connect(function()
		if tick() - LastUpdate >= UpdateInterval then
			LastUpdate = tick()
			Update()
		end
	end)
end)()


local Cooldowns = {}
game.Players.PlayerRemoving:Connect(function(Player)
	if Cooldowns[Player.UserId] ~= nil then
		if tick() - Cooldowns[Player.UserId] < RemoteFunctionCooldown then
			wait(RemoteFunctionCooldown - (tick() - Cooldowns[Player.UserId]))
			Cooldowns[Player.UserId] = nil
		else
			Cooldowns[Player.UserId] = nil
		end
	end
end)


CastEmpRemote.OnServerInvoke = function(Player)
	if Cooldowns[Player.UserId] ~= nil then
		if tick() - Cooldowns[Player.UserId] < RemoteFunctionCooldown then
			return
		else
			Cooldowns[Player.UserId] = tick()
		end
	else
		Cooldowns[Player.UserId] = tick()
	end
	local Character = Player.Character
	if Character then
		local Hrp = Character:FindFirstChild("HumanoidRootPart")
		if Hrp then
			local EmpSFX = Instance.new("Sound")
			EmpSFX.SoundId = EmpSound
			EmpSFX.Volume = EmpSound_Volume
			EmpSFX.RollOffMaxDistance = EmpSound_RollOffMaxDistance
			EmpSFX.PlaybackSpeed = EmpSound_PlaybackSpeed
			EmpSFX.Parent = Hrp
			if not EmpSFX.IsLoaded then
				EmpSFX.Loaded:Wait()
			end
			EmpSFX.Ended:Connect(function() EmpSFX:Destroy() end)
			EmpSFX:Play()
			local Hitbox = Instance.new("Part")
			Hitbox.Name = "Emp_Hitbox_"..Player.Name
			Hitbox.Size = EmpHitboxSize
			Hitbox.Transparency = 1
			Hitbox.CanCollide = false
			Hitbox.Anchored = true
			Hitbox.Parent = workspace
			Hitbox.Touched:Connect(function() end)
			Hitbox.Position = Hrp.Position
			local TouchingParts = Hitbox:GetTouchingParts()
			Hitbox:Destroy()
			local VisualBox = Instance.new("Part")
			VisualBox.Size = Vector3.new(0, 0, 0)
			VisualBox.Anchored = true
			VisualBox.Color = VisualEmpColor
			VisualBox.Position = Hrp.Position
			VisualBox.CanCollide = false
			local Mesh = Instance.new("SpecialMesh")
			Mesh.MeshType = Enum.MeshType.Sphere
			Mesh.Parent = VisualBox
			VisualBox.Transparency = math.clamp(VisualEmpTransparency, 0, 1)
			VisualBox.Parent = workspace
			local Tween = game:GetService("TweenService"):Create(VisualBox, TweenInfo.new(VisualEmpLifetime/2), {Size = EmpHitboxSize})
			local Tween2 = game:GetService("TweenService"):Create(VisualBox, TweenInfo.new(VisualEmpLifetime/2), {Size = Vector3.new(0, 0, 0)})
			Tween2.Completed:Connect(function()
				VisualBox:Destroy()
			end)
			Tween.Completed:Connect(function()
				Tween2:Play()
			end)
			Tween:Play()
			for i = 1, #TouchingParts do
				if i >= MaxHitboxIndex then break end
				local Descendants = TouchingParts[i]:GetDescendants()
				for _i = 1, #Descendants do
					if _i >= MaxDescendantsIndex then break end
					if Descendants[_i]:IsA("Light") then
						local Result = Find(Descendants[_i])
						if Result == nil then
							local EmitterParent = Descendants[_i]:FindFirstAncestorWhichIsA("BasePart")
							local NewEmitter = nil
							if EmitterParent then
								NewEmitter = DisabledEmitter:Clone()
								NewEmitter.Parent = EmitterParent
							end
							table.insert(DisabledLights, {Descendants[_i], tick() + DisabledTime, NewEmitter})
						else
							DisabledLights[Result][2] = tick() + DisabledTime
						end
					end
				end
			end
		end
	end
end

Emp Client

local EmpRemote = game.ReplicatedStorage:WaitForChild("CastEmp")
local UserInputService = game:GetService("UserInputService")


local function onInputBegan(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Q then
			print("Emp Client: Invoking Emp")
			EmpRemote:InvokeServer()
		end
	end
end


UserInputService.InputBegan:Connect(onInputBegan)
print("Emp Client: Script Ready -- Press Q")

Emp.rbxl (50.4 KB)

Thanks a lot! I can use this to better learn how to use coroutines and the RunService and improve my future scripts!