Region3-based tool restriction not working

Can anyone find anything wrong with this script? It is supposed to give a player a gun when they enter a region and remove it when they leave. It used to work and still kinda works but the gun keeps getting removed and readded whenever the player moves around (they aren’t leaving the region). There are no errors in output.

local Campsite = script.Parent.Campsite
local Pos1 = Campsite.Position - (Campsite.Size / 2)
local Pos2 = Campsite.Position + (Campsite.Size / 2)
local Region = Region3.new(Pos1, Pos2)

game.Teams.Campers.PlayerAdded:Connect(function(player)
	if player:GetAttribute("Property") == script.Parent.Name then
		local Character = player.Character:WaitForChild("HumanoidRootPart")
		local Backpack = player.Backpack
		while wait() do
			local partsInRegion = workspace:FindPartsInRegion3(Region)
			if table.find(partsInRegion, Character) and #Backpack:GetChildren() == 0 and not Character.Parent:FindFirstChild("Gun") then
				game.ServerStorage.Tools.Gun:Clone().Parent = Backpack
			elseif not table.find(partsInRegion, Character) then
				Backpack:ClearAllChildren()
				for _, object in pairs(Character.Parent:GetChildren()) do if object:IsA("Tool") then object:Destroy() end end
			end
		end
	end
end)

workspace:FindPartsInRegion3 has 3 parameters. The first is the region3 you want to use, the second is an instance you wish to ignore, and the last is the max amount of parts that are returned. You aren’t giving the function any parameters, other than the region3, so it’s only returning you a table of 20 parts. What is happening is there are more than 20 parts in the region, therefore some are left out.

If you want a quick fix, just do workspace:FindPartsInRegion3(Region, nil, 100000). If you want a better fix, create a table of all the player’s HumanoidRootParts and use workspace:FindPartsInRegion3WithWhiteList(Region, playerTable, #playerTable)

This makes sense. What caused this error to occur was adding decoration to the inside of the region, so it makes sense that this caused there to be more than 20 parts. Thank you for your help.

It’s because the while loop is yielding the script so it should be break to continue so I dont recommend you put it in PlayerAdded but put it in the workspace or serverscriptStorage or use couroutine